Пример #1
0
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)
}
Пример #2
0
func (op *qiankeOp) register(opName string, handler HandlerFunc) {
	if _, ok := op.opHandlers[opName]; ok {
		dlog.EPrintf("duplicate files op %s handler registered!\n", opName)
		return
	}
	op.opHandlers[opName] = handler
}
Пример #3
0
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)
	}
}
Пример #4
0
func (fo *FileOp) register(op string, handler HandlerFunc) {
	if _, ok := fo.opHandlers[op]; ok {
		dlog.EPrintf("duplicate files op %s handler registered!\n", op)
		return
	}
	fo.opHandlers[op] = handler
}
Пример #5
0
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
}
Пример #6
0
func (op *qiankeOp) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	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:]
	qkReq := op.getAvalibleReq()
	qkReq.out = w
	qkReq.req = req
	qkReq.op = op
	defer func() {
		op.recycle(qkReq)
		req.Body.Close()
	}()

	if handler, ok := op.opHandlers[action]; ok {
		handler(qkReq)
	} else {
		dlog.EPrintf("path %s handler not found!\n", req.URL.Path)
		http.NotFound(w, req)
	}
}
Пример #7
0
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:64
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.out = w
	unReq.req = req
	unReq.op = op
	defer func() {
		op.recycle(unReq)
		req.Body.Close()
	}()
//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:88

//line /root/myopensrc/ornet/anyd/src/service/sandbox/sandbox.go:87
}
Пример #8
0
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
	}
}
Пример #9
0
func (op *Url2NameOp) 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.out = w
	unReq.req = req
	unReq.op = op
	defer func() {
		op.recycle(unReq)
		req.Body.Close()
	}()

	if handler, ok := op.opHandlers[action]; ok {
		handler(unReq)
	} else {
		http.NotFound(w, req)
	}
}
Пример #10
0
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)
	}
}
Пример #11
0
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)
}
Пример #12
0
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)
	}
}
Пример #13
0
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)
}
Пример #14
0
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()
	}
}