|
|
|
@@ -3,6 +3,7 @@ package main |
|
|
|
import ( |
|
|
|
"biukop/sfm/loan" |
|
|
|
"database/sql" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
log "github.com/sirupsen/logrus" |
|
|
|
"net/http" |
|
|
|
@@ -33,12 +34,11 @@ func apiV1Main(w http.ResponseWriter, r *http.Request) { |
|
|
|
//try session login first, if not an empty session will be created |
|
|
|
session, e := apiV1InitSession(r) |
|
|
|
if e != nil { |
|
|
|
log.Warn("Fail to InitSession %+v", session) |
|
|
|
apiV1ServerError(w, r) |
|
|
|
log.Warnf("Fail to InitSession %+v", session) |
|
|
|
apiV1Client403Error(w, r) |
|
|
|
return |
|
|
|
} |
|
|
|
session.RenewIfExpireSoon() |
|
|
|
session.SetRemote(r) |
|
|
|
|
|
|
|
//we have a session now, either guest or valid user |
|
|
|
//search through handler |
|
|
|
@@ -64,14 +64,16 @@ func apiV1InitSession(r *http.Request) (session loan.Session, e error) { |
|
|
|
if e == nil { //we got existing session |
|
|
|
e = session.ValidateRequest(r) |
|
|
|
if e != nil { // not successfully validated |
|
|
|
log.Warn("failed session login %+v, %s", session, time.Now().Format("RFC1132")) |
|
|
|
log.Warnf("failed session login %+v, %s", session, time.Now().Format(time.RFC1123)) |
|
|
|
session.InitGuest(time.Now().Add(loan.DefaultSessionDuration)) |
|
|
|
e = nil |
|
|
|
} //else, we have logged this user in |
|
|
|
} else if e == sql.ErrNoRows { |
|
|
|
log.Warn("DB has no corresponding session ", sid) |
|
|
|
session.InitGuest(time.Now().Add(loan.DefaultSessionDuration)) |
|
|
|
e = nil //we try to init an empty one |
|
|
|
} else { |
|
|
|
log.Warn("Retrieve Session %s encountered error %s", sid, e.Error()) |
|
|
|
log.Warnf("Retrieve Session %s encountered error %s", sid, e.Error()) |
|
|
|
} |
|
|
|
session.SetRemote(r) //make sure they are using latest remote |
|
|
|
return |
|
|
|
@@ -83,13 +85,29 @@ func apiV1ErrorCheck(e error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func apiV1ServerError(w http.ResponseWriter, r *http.Request) { |
|
|
|
func apiV1Server500Error(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
|
|
|
w.WriteHeader(500) |
|
|
|
fmt.Fprintf(w, "Server Internal Error "+time.Now().Format("RFC1132")) |
|
|
|
fmt.Fprintf(w, "Server Internal Error "+time.Now().Format(time.RFC1123)) |
|
|
|
|
|
|
|
//write log |
|
|
|
dump := logRequestDebug(httputil.DumpRequest(r, true)) |
|
|
|
dump = strings.TrimSpace(dump) |
|
|
|
log.Warn("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) |
|
|
|
log.Warnf("Unhandled Protocol = %s path= %s", r.Method, r.URL.Path) |
|
|
|
} |
|
|
|
|
|
|
|
func apiV1Client403Error(w http.ResponseWriter, r *http.Request) { |
|
|
|
w.WriteHeader(403) |
|
|
|
type struct403 struct { |
|
|
|
Error int |
|
|
|
ErrorMsg string |
|
|
|
} |
|
|
|
e403 := struct403{Error: 403, ErrorMsg: "Not Authorized " + time.Now().Format(time.RFC1123)} |
|
|
|
msg403, _ := json.Marshal(e403) |
|
|
|
fmt.Fprintln(w, string(msg403)) |
|
|
|
|
|
|
|
//write log |
|
|
|
dump := logRequestDebug(httputil.DumpRequest(r, true)) |
|
|
|
dump = strings.TrimSpace(dump) |
|
|
|
log.Warnf("Not authorized http(%s) path= %s, %s", r.Method, r.URL.Path, dump) |
|
|
|
} |