func (fo *FileOp) Init(config interface{}) (err error) { dlog.Printf("FileOp init...\n") fo.FileOpConfig = config.(*FileOpConfig) fo.opHandlers = make(map[string]HandlerFunc, 0) fo.opReqPool = make(chan *FileRequest, fo.RequestPoolSize) for i := 0; i < fo.RequestPoolSize; i++ { req := &FileRequest{} fo.opReqPool <- req } fo.client = redis.NewTCPClient(&redis.Options{Addr: fo.RedisAddr, DialTimeout: time.Millisecond * 100}) val, err := fo.client.Ping().Result() if err != nil { dlog.EPrintln(err.Error()) return err } if val != "PONG" { dlog.EPrintln("redis returned ping result invalid!") return errors.New("redis returned ping result invalid!") } fo.register("upload", uploadHandler) fo.register("download", downloadHandler) fo.register("ping", pingHandler) dlog.Printf("FileOp init ok, config:(%+v)\n", fo.FileOpConfig) return nil }
func registerHandler(qkReq *qiankeRequest) { req := qkReq.req w := qkReq.out dbOp := qkReq.op.dbOp user := &QKUser{} data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Printf("data:%s\n", string(data)) err = json.Unmarshal(data, user) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } err = dbOp.RegisterUser(user) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } return }
func (op *Url2NameOp) Init(config interface{}) (err error) { dlog.Printf("Url2NameOp init...\n") op.Url2NameOpConfig = config.(*Url2NameOpConfig) op.opHandlers = make(map[string]HandlerFunc, 0) op.opReqPool = make(chan *Url2NameRequest, op.RequestPoolSize) for i := 0; i < op.RequestPoolSize; i++ { req := &Url2NameRequest{} op.opReqPool <- req } op.entryPool = make(chan *Url2NameEntry, op.EntryPoolSize) for i := 0; i < op.RedisWriteCount; i++ { go writeToRedis(op) } err = os.MkdirAll(op.DownPath, 0755) if err != nil { dlog.EPrintln(err.Error()) return err } err = os.MkdirAll(op.StorePath, 0755) if err != nil { dlog.EPrintln(err.Error()) return err } op.register("add", addHandler) op.register("rm", removeHandler) op.register("help", helpHandler) // http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100 dlog.Printf("Url2NameOp init ok, config:(%+v)\n", op.Url2NameOpConfig) return nil }
func SafeHandler(handler http.Handler) http.HandlerFunc { return func(w http.ResponseWriter, req *http.Request) { dlog.Printf("%(+v)\n", req) defer func() { if err, ok := recover().(error); ok { http.Error(w, err.Error(), http.StatusInternalServerError) dlog.EPrintln("WARN: panic in %v - %v", handler, err) dlog.EPrintln(string(debug.Stack())) } }() handler.ServeHTTP(w, req) } }
func (op *QkDBOp) Init(mysqlHost, mysqlUsername, mysqlPasswd, dbName string) error { op.db, op.err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s", mysqlUsername, mysqlPasswd, mysqlHost, dbName)) if op.err != nil { dlog.EPrintln(op.err.Error()) return op.err } op.err = op.db.Ping() if op.err != nil { dlog.EPrintln(op.err.Error()) return op.err } return nil }
//export WriteData func WriteData(ptr unsafe.Pointer, data *C.char, n C.int) C.int { reqCtx := (*SandBoxRequest)(ptr) // req := reqCtx.req if reqCtx == nil { dlog.EPrintln("invalid request context pointer!") return -1 } w := reqCtx.w written, err := w.Write([]byte(C.GoStringN(data, n))) if err != nil { dlog.EPrintln(err.Error()) return -1 } return C.int(written) }
func addHandler(unReq *Url2NameRequest) { req := unReq.req w := unReq.out op := unReq.op if req.Method != "POST" { dlog.EPrintf("invalid request method.\n") http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } entry := &Url2NameEntry{} err = json.Unmarshal(data, entry) if err != nil { dlog.EPrintf(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest) return } op.entryPool <- entry w.WriteHeader(http.StatusOK) }
func (fo *FileOp) ServeHTTP(w http.ResponseWriter, req *http.Request) { dlog.Printf("(%+v)\n", *req) pos := strings.LastIndex(req.URL.Path, "/") if pos == -1 { dlog.EPrintf("invalid request :%s\n", req.RequestURI) http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) return } action := req.URL.Path[pos+1:] foReq := fo.getAvalibleReq() foReq.out = w foReq.req = req foReq.op = fo defer func() { fo.recycle(foReq) req.Body.Close() }() if handler, ok := fo.opHandlers[action]; ok { handler(foReq) } else { dlog.EPrintln("bad request!") http.NotFound(w, req) } }
func (fi *UploadFileInfo) setInfo(field string, val string) { if strings.HasSuffix(field, "md5") { fi.FileMd5 = val } else if strings.HasSuffix(field, "size") { filesize, err := strconv.Atoi(val) if err != nil { dlog.EPrintln(err.Error()) } fi.FileSize = filesize } else if strings.HasSuffix(field, "path") { fi.UploadPath = val } else if strings.HasSuffix(field, "content_type") { fi.ContentType = val } else if strings.HasSuffix(field, "name") { fi.FileName = val } else { dlog.EPrintln("invalid SaveInfo filed type.") } }
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:50 func WriteData(ptr unsafe.Pointer, data *_Ctype_char, n int) _Ctype_int { reqCtx := *SandBoxRequest(ptr) req := reqCtx.req w := reqCtx.w n, err := w.Write(_Cfunc_GoStringN(data, n)) if err != nil { dlog.EPrintln(err.Error()) return -1 } return n }
//export ReadBodyData //returned data need to be freed after no use. func ReadBodyData(ptr unsafe.Pointer) (body *C.char, n int) { reqCtx := (*SandBoxRequest)(ptr) req := reqCtx.req data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) return C.CString("nil"), -1 } return C.CString(string(data)), len(data) }
func InitHandlerConf(confs map[string]toml.Primitive, md *toml.MetaData) error { var err error for k, handler := range CmdHandlers { if conf, ok := confs[k]; ok { handlerConf := handler.ConfigStruct() err = md.PrimitiveDecode(conf, handlerConf) if err != nil { dlog.EPrintln(err.Error()) return err } err = handler.Init(handlerConf) if err != nil { dlog.EPrintln(err.Error()) return err } } } // CmdServerMux.Handle("ping", http.HandlerFunc(PingHandler)) return err }
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:37 func ReadBodyData(ptr unsafe.Pointer) (body *_Ctype_char, n int) { reqCtx := *SandBoxRequest(ptr) req := reqCtx.req data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) return _Cfunc_CString("nil"), -1 } //line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:47 //line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:46 return _Cfunc_CString(string(data)), len(data) }
func (op *QkDBOp) RegisterUser(userInfo *QKUser) error { res, err := op.db.Exec(fmt.Sprintf(`insert into qkuser(email,passwd,cellphone) values('%s', '%s', '%s')`, userInfo.Email, userInfo.Password, userInfo.CellPhone)) if err != nil { op.err = err dlog.EPrintln(err.Error()) return err } rows, err := res.RowsAffected() if rows == 0 || err != nil { dlog.EPrintf("register new user failed", err.Error()) return errors.New("register new user failed.") } return nil }
func loginHandler(qkReq *qiankeRequest) { req := qkReq.req w := qkReq.out // op := qkReq.op data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Printf("data:%s\n", string(data)) fmt.Printf("req:%+v\n", req) w.Write([]byte("test response.")) return }
func (op *SandboxOp) Init(config interface{}) (err error) { dlog.Printf("SandboxOp init...\n") op.SandboxOpConfig = config.(*SandboxOpConfig) op.opReqPool = make(chan *SandBoxRequest, op.RequestPoolSize) for i := 0; i < op.RequestPoolSize; i++ { req := &SandBoxRequest{} op.opReqPool <- req } // http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 100 op.luaCtx = C.init_lua() luaFilename := C.CString(op.LuaFilename) defer C.free(unsafe.Pointer(luaFilename)) _, err = C.load_lua_file(op.luaCtx, luaFilename) if err != nil { dlog.EPrintln(err.Error()) return err } dlog.Printf("SandboxOp init ok, config:(%+v)\n", op.SandboxOpConfig) return nil }
func (op *SandboxOp) ServeHTTP(w http.ResponseWriter, req *http.Request) { dlog.Printf("(%+v)\n", *req) pos := strings.LastIndex(req.URL.Path, "/") if pos == -1 { dlog.EPrintf("invalid request :%s\n", req.RequestURI) http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden) return } // action := req.URL.Path[pos+1:] unReq := op.getAvalibleReq() unReq.w = w unReq.req = req unReq.op = op defer func() { op.recycle(unReq) req.Body.Close() }() _, err := C.process_request(op.luaCtx, unsafe.Pointer(unReq)) if err != nil { dlog.EPrintln(err.Error()) return } }
func (op *qiankeOp) Init(config interface{}) (err error) { dlog.Println("qiankeOp init...") op.qiankeOpConfig = config.(*qiankeOpConfig) op.dbOp = new(QkDBOp) err = op.dbOp.Init(op.MysqlHost, op.MysqlUsername, op.MysqlPasswd, op.MysqlDbName) if err != nil { dlog.EPrintln(err.Error()) return err } op.opReqPool = make(chan *qiankeRequest, op.RequestPoolSize) op.opHandlers = make(map[string]HandlerFunc, 0) for i := 0; i < op.RequestPoolSize; i++ { req := &qiankeRequest{} op.opReqPool <- req } op.register("login", loginHandler) op.register("register", registerHandler) dlog.Printf("qiankeOp init ok, config:%+v\n", op.qiankeOpConfig) return nil }
func writeToRedis(op *Url2NameOp) { redisClient := redis.NewTCPClient(&redis.Options{Addr: op.RedisAddr, DialTimeout: time.Millisecond * 200}) defer redisClient.Close() // var tr http.RoundTripper = &http.Transport{ // Dial: TimeoutDialer(time.Second*5, op.MaxDownloadTime.Duration)} // httpClient := &http.Client{Transport: tr} for entry := range op.entryPool { urlWithoutScheme := strings.TrimLeft(entry.Url, "http://") getResponse := redisClient.HMGet(Url2NameKey, urlWithoutScheme) result, err := getResponse.Result() if err != nil { dlog.EPrintln(err.Error()) continue } if len(result) != 0 && result[0] != nil { dlog.EPrintf("url %s duplicate! value:%s\n", urlWithoutScheme, result[0].(string)) continue } // res, err := httpClient.Get(entry.Url) // if err != nil { // dlog.EPrintln(err.Error()) // continue // } // if res.StatusCode != 200 { // dlog.EPrintf("download data from %s failed, error code :%d\n", // entry.Url, res.StatusCode) // continue // } // defer res.Body.Close() // downloadFileName := path.Join(op.DownPath, entry.AppName) // file, err := os.Create(downloadFileName) // if err != nil { // dlog.EPrintln(err.Error()) // continue // } // defer file.Close() // written, err := io.Copy(file, res.Body) // if err != nil { // dlog.EPrintln(err.Error()) // continue // } // if res.ContentLength >= 0 && res.ContentLength != written { // dlog.EPrintf("url %s body data not equal to content-length\n", entry.Url) // continue // } zaddResponse := redisClient.ZAdd(UrlTimeKey, redis.Z{Score: float64(time.Now().Unix()), Member: urlWithoutScheme}) _, err = zaddResponse.Result() if err != nil { dlog.EPrintln(err.Error()) continue } setResponse := redisClient.HMSet(Url2NameKey, urlWithoutScheme, entry.AppName) _, err = setResponse.Result() if err != nil { dlog.EPrintln(err.Error()) continue } // storeFileName := path.Join(op.StorePath, entry.AppName) // err = os.Rename(downloadFileName, storeFileName) // if err != nil { // dlog.EPrintln(err.Error()) // } redisClient.Save() } }
func directUploadHandler(foReq *FileRequest) { req := foReq.req w := foReq.out var fileSize int64 if contentLen, ok := req.Header["Content-Length"]; ok { contentLength, err := strconv.Atoi(contentLen[0]) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } fileSize = int64(contentLength) } else { dlog.EPrintf("invalid request header\n") http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } if fileSize <= 0 { dlog.EPrintf("invalid request header\n") http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } values, err := url.ParseQuery(req.URL.RawQuery) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } if filename, ok := values["filename"]; ok { tmpFilePath := path.Join(foReq.op.UploadPath, filename[0]) localWriter, err := os.Create(tmpFilePath) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } // defer localWriter.Close() writeLen, err := io.CopyN(localWriter, req.Body, fileSize) if writeLen != fileSize { localWriter.Close() os.Remove(tmpFilePath) dlog.EPrintf("content size is not equal to content-length field value.") http.Error(w, "body size not equal to content-length field value.", http.StatusBadRequest) return } localWriter.Close() fullFilePath := path.Join(foReq.op.StorePath, filename[0]) err = os.Rename(tmpFilePath, fullFilePath) if err != nil { os.Remove(tmpFilePath) dlog.EPrintf("rename file %s to file %s failed.", tmpFilePath, fullFilePath) http.Error(w, "rename file failed.", http.StatusInternalServerError) return } //todo: } else { dlog.EPrintf("request bad parameter\n") http.Error(w, "bad parameter", http.StatusBadRequest) } }
func fastcgiUploadHandler(foReq *FileRequest) { req := foReq.req w := foReq.out // contentType, ret := req.Header["Content-Type"] // if ret == false { // dlog.EPrintf() // http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) // return // } // mediaType, params, err := mime.ParseMediaType(contentType[0]) // if err != nil { // http.Error(w, err.Error(), http.StatusInternalServerError) // return // } // fullFilePath := "" // if strings.HasPrefix(mediaType, "multipart/") { // reader, err := req.MultipartReader() // if err != nil { // dlog.EPrintln(err.Error()) // http.Error(w, err.Error(), http.StatusBadRequest) // return // } // // mr := multipart.NewReader(req.Body, params["boundary"]) // mf, err := reader.ReadForm(0) // if err != nil { // dlog.EPrintln(err.Error()) // http.Error(w, err.Error(), http.StatusInternalServerError) // return // } values, err := url.ParseQuery(req.URL.RawQuery) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } filename, ok := values["filename"] if !ok { dlog.EPrintf("request without specify filename paramter!\n") http.Error(w, err.Error(), http.StatusBadRequest) return } err = req.ParseMultipartForm(32 * 1024) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest) return } var infoList []UploadFileInfo dlog.Printf("(%+v)\n", req.Form) for k, v := range req.Form { if infoList == nil { infoList = make([]UploadFileInfo, len(v)) } for i := 0; i < len(v); i++ { infoList[i].setInfo(k, v[i]) } } dlog.Printf("(%+v)\n", infoList) // fmt.Println(infoList) for _, fi := range infoList { fullFilePath := path.Join(foReq.op.StorePath, filename[0]) err = os.Rename(fi.UploadPath, fullFilePath) dlog.Printf("rename file %s to file %s\n", fi.UploadPath, fullFilePath) if err != nil { dlog.EPrintf(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } } // } w.WriteHeader(http.StatusOK) }
func downloadHandler(foReq *FileRequest) { dlog.Println("downloadHandler in") w := foReq.out req := foReq.req fo := foReq.op values, err := url.ParseQuery(req.URL.RawQuery) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } if reqUrl, ok := values["link"]; ok { if reqUrl[0] == "" { dlog.EPrintf("invalid link parameter,from remote %s, url:%s\n", req.RemoteAddr, req.URL.Path) http.Error(w, "invalid link parameter", http.StatusBadRequest) return } // realUrl, err := url.QueryUnescape(reqUrl[0]) // if err != nil { // dlog.EPrintln(err.Error()) // http.Error(w, err.Error(), http.StatusInternalServerError) // return // } hmResponse := fo.client.HMGet(Url2NameKey, reqUrl[0]) result, err := hmResponse.Result() if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } if len(result) == 0 || result[0] == nil { dlog.EPrintf("url %s not found!\n", reqUrl[0]) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } //just update url visit time. fo.client.ZAdd(UrlTimeKey, redis.Z{Score: float64(time.Now().Unix()), Member: reqUrl[0]}) if len(result) == 0 || result[0] == nil { dlog.EPrintf("url2name failed, url:%s\n", reqUrl[0]) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) return } filename := result[0].(string) // fmt.Println(filename) urlRes, err := url.Parse(reqUrl[0]) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } realFilename := "" if path.Ext(filename) == "" { realFilename = fmt.Sprintf("%s%s", filename, path.Ext(urlRes.Path)) } else { realFilename = filename } w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", realFilename)) w.Header().Add("Content-Type", "application/octet-stream") fullFilePath := path.Join(foReq.op.StorePath, filename) if err = exists(fullFilePath); err != nil { fullFilePath = path.Join(foReq.op.StorePath, realFilename) if err = exists(fullFilePath); err != nil { dlog.EPrintf("file %s not found\n", fullFilePath) http.NotFound(w, req) return } } dlog.Printf("download file %s\n", fullFilePath) http.ServeFile(w, req, fullFilePath) //todo: } else { dlog.EPrintf("request bad parameter\n") http.Error(w, "bad parameter", http.StatusBadRequest) } }
func removeHandler(unReq *Url2NameRequest) { req := unReq.req w := unReq.out op := unReq.op if req.Method != "POST" { dlog.EPrintf("invalid request method.\n") http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } data, err := ioutil.ReadAll(req.Body) if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } entry := &Url2NameEntry{} err = json.Unmarshal(data, entry) if err != nil { dlog.EPrintf(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest) return } urlWithoutScheme := strings.TrimLeft(entry.Url, "http://") redisClient := redis.NewTCPClient(&redis.Options{Addr: op.RedisAddr, DialTimeout: time.Millisecond * 200}) defer redisClient.Close() // getResponse := redisClient.HMGet(Url2NameKey, urlWithoutScheme) // result, err := getResponse.Result() // if err != nil { // dlog.EPrintln(err.Error()) // http.Error(w, err.Error(), http.StatusInternalServerError) // return // } // if len(result) == 0 || result[0] == nil { // dlog.EPrintf("url %s not found!\n", urlWithoutScheme) // http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) // return // } // filename := result[0].(string) delResponse := redisClient.HDel(Url2NameKey, urlWithoutScheme) _, err = delResponse.Result() if err != nil { dlog.EPrintf(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } zRemResponse := redisClient.ZRem(UrlTimeKey, urlWithoutScheme) _, err = zRemResponse.Result() if err != nil { dlog.EPrintln(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } redisClient.Save() // delFileName := path.Join(op.StorePath, filename) // err = os.Remove(delFileName) // if err != nil { // dlog.EPrintln(err.Error()) // http.Error(w, err.Error(), http.StatusInternalServerError) // return // } w.WriteHeader(http.StatusOK) }