Example #1
0
func View(this *ViewController, loadImgRequest *request.LoadImgRequest) {
	var result = util.Error{}
	Cat := cat.Instance()
	title := "/fdupload/target"
	if loadImgRequest.IsSource {
		title = "/fdupload/source"
	}
	tran := Cat.NewTransaction("URL", title)
	defer func() {
		if p := recover(); p != nil {
			Cat.LogPanic(p)
		}
		if result.Err != nil {
			tran.SetStatus(result.Err)
		} else {
			tran.SetStatus("0")
		}
		tran.Complete()
	}()
	util.LogEvent(Cat, "URL", "URL.Client", map[string]string{
		"clientip": util.GetClientIP(this.Ctx.Request),
		"serverip": util.GetIP(),
		"proto":    this.Ctx.Request.Proto,
		"referer":  this.Ctx.Request.Referer(),
		//"agent":    request.UserAgent(),
	})
	util.LogEvent(Cat, "URL", "URL.Method", map[string]string{
		"Http": this.Ctx.Request.Method + " " + loadImgRequest.FilePath,
	})

	imgRequest := business.ImageRequest{}
	imgRequest.Cat = Cat
	resp, e := imgRequest.Download(loadImgRequest)

	if e.Err != nil {
		this.Ctx.WriteString(e.Err.(error).Error())
	} else {
		//this.Ctx.Output.ContentType("image/Jpeg")

		//this.Ctx.Output.Body(resp.FileBytes)
		bts, err := soapparse.B64.DecodeString(string(resp.FileBytes))
		if err != nil {
			this.Ctx.WriteString(err.Error())
		} else {
			this.Ctx.Output.Header("Content-Type", "image/jpeg")
			this.Ctx.Output.Body(bts)
		}
	}
}
Example #2
0
func (handler *LogoWS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	Cat := cat.Instance()
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("Content-Type", "text/xml")

	tran := Cat.NewTransaction("URL", "LogoWs.asmx")
	util.LogEvent(Cat, "URL", "URL.Client", map[string]string{
		"clientip": util.GetClientIP(r),
		"serverip": util.GetIP(),
		"proto":    r.Proto,
		"referer":  r.Referer(),
		//"agent":    request.UserAgent(),
	})
	var result util.Error
	defer func() {
		if p := recover(); p != nil {
			Cat.LogPanic(p)
		}
		if result.Err != nil && !result.IsNormal {
			tran.SetStatus(result.Err)
		} else {
			tran.SetStatus("0")
		}
		tran.Complete()
	}()

	bts, err := ioutil.ReadAll(r.Body)
	if err != nil {
		util.LogErrorEvent(Cat, "RequestReadError", err.Error())
		result = util.Error{IsNormal: false, Err: err, Type: "RequestReadError"}

		return
	}
	req := request.Request{}
	result = EncodeRequest(Cat, bts, &req)
	if result.Err != nil && !result.IsNormal {
		//writeResponse(w, []byte(result.Type))
		return
	}

	var (
		resp        interface{}
		header      *response.Header
		e           util.Error
		logoRequest = business.LogoRequest{
			ImageRequest: business.ImageRequest{
				Cat: Cat,
			},
			FontSizes: []int{14, 16, 18, 20},
		}
	)

	requestTran := Cat.NewTransaction("Request", "SaveLogo")
	defer func() {
		if p := recover(); p != nil {
			Cat.LogPanic(p)
		}
		if result.Err != nil && !result.IsNormal {
			requestTran.SetStatus(result.Err)
		} else {
			requestTran.SetStatus("0")
		}
		requestTran.Complete()
	}()

	resp, result = logoRequest.Save(&req.SaveRequest)

	if result.Err != nil {
		header = createFailHeader(req.Header, fmt.Sprintf("%v", result.Err))
	} else {
		header = transformHeader(req.Header, RESULTCODE_SUCCESS, "")
	}
	content, e := DecodeResponse(Cat, header, resp)
	if e.Err != nil {
		result = e
		writeResponse(w, []byte(result.Err.(error).Error()))
	} else {
		writeResponse(w, content)
	}

	//content, err := soapparse.DecResp(header, resp) //
	//if err != nil {
	//	return
	//}
	//writeResponse(w, content)
}
Example #3
0
func (handler *ImageWS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("Content-Type", "text/xml")

	Cat := cat.Instance()
	tran := Cat.NewTransaction("URL", "ImageWs.asmx")
	util.LogEvent(Cat, "URL", "URL.Client", map[string]string{
		"clientip": util.GetClientIP(r),
		"serverip": util.GetIP(),
		"proto":    r.Proto,
		"referer":  r.Referer(),
		//"agent":    request.UserAgent(),
	})
	var result util.Error
	defer func() {
		if p := recover(); p != nil {
			Cat.LogPanic(p)
		}
		if result.Err != nil && !result.IsNormal {
			tran.SetStatus(result.Err)

		} else {
			tran.SetStatus("0")
		}
		tran.Complete()
	}()
	bts, err := ioutil.ReadAll(r.Body)
	if err != nil {
		util.LogErrorEvent(Cat, "RequestReadError", err.Error())
		result = util.Error{IsNormal: false, Err: err, Type: "RequestReadError"}
		msg := []byte(err.Error())
		writeResponse(w, msg)
		return
	}
	req := request.Request{}
	result = EncodeRequest(Cat, bts, &req)
	if result.Err != nil && !result.IsNormal {
		writeResponse(w, []byte(result.Type))
		return
	}
	var (
		resp       interface{}
		header     *response.Header
		e          util.Error
		imgRequest = business.ImageRequest{Cat: Cat}
	)

	requestTran := Cat.NewTransaction("Request", strings.Replace(req.Header.RequestType, "Arch.Base.ImageWS.", "", -1))
	defer func() {
		if p := recover(); p != nil {
			Cat.LogPanic(p)
		}
		if result.Err != nil && !result.IsNormal {
			requestTran.SetStatus(result.Err)
		} else {
			requestTran.SetStatus(CATTRANSUCCESS)
		}
		requestTran.Complete()
	}()

	switch req.Header.RequestType {
	case REQUESTTYPE_SAVEIMAGE:
		resp, e = imgRequest.Save(&req.SaveRequest)
	case REQUESTTYPE_LOADIMAGE:
		resp, e = imgRequest.Download(&req.LoadImgRequest)
	case REQUESTTYPE_LOADZIP:
		resp, e = imgRequest.DownloadZip(&req.LoadZipRequest)
	case REQUESTTYPE_DELETEIMAGE:
		resp, e = imgRequest.Delete(&req.DeleteRequest)
	default:
		util.LogErrorEvent(Cat, "RequestTypeInvalid", req.Header.RequestType)
		e = util.Error{IsNormal: true, Err: errors.New("requesttype is invalid!"), Type: "RequestTypeInvalid"}
	}
	if e.Err != nil {
		result = e
		header = createFailHeader(req.Header, fmt.Sprintf("%v", e.Err))
	} else {
		header = transformHeader(req.Header, RESULTCODE_SUCCESS, "")
	}
	content, e := DecodeResponse(Cat, header, resp)
	if e.Err != nil {
		result = e
		writeResponse(w, []byte(result.Err.(error).Error()))
	} else {
		writeResponse(w, content)
	}
}
Example #4
0
func (handler *Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	Cat := cat.Instance()
	handler.ChainBuilder = &ProcChainBuilder{Cat}
	uri := request.URL.String()
	tran := Cat.NewTransaction("URL", getShortUri(uri))
	var (
		err       error
		isSuccess bool = true
	)
	defer func() {
		p := recover()
		if p != nil {
			logErrWithUri(uri, fmt.Sprintf("%v", p), "errorLevel")
			Cat.LogPanic(p)
			tran.SetStatus(p)
		}

		if isSuccess {
			tran.SetStatus("0")
			tran.Complete()
		} else {
			tran.SetStatus(err)
			tran.Complete()
		}
		if p != nil || err != nil {
			http.Error(writer, http.StatusText(404), 404)
		}
	}()

	LogEvent(Cat, "URL", "URL.Client", map[string]string{
		"clientip": util.GetClientIP(request),
		"serverip": util.GetIP(),
		"proto":    request.Proto,
		"referer":  request.Referer(),
		//"agent":    request.UserAgent(),
	})

	LogEvent(Cat, "URL", "URL.Method", map[string]string{
		"Http": request.Method + " " + uri,
	})

	LogEvent(Cat, "UpstreamProcess", JoinString(GetIP(), ":", WorkerPort), nil)

	isDigimarkUrl := false
	params, ok1 := legalUrl.FindStringSubmatchMap(uri)
	if !ok1 {
		params, ok1 = digimarkUrl.FindStringSubmatchMap(uri)
		if ok1 {
			isDigimarkUrl = true
		} else {
			err = errors.New("URI.ParseError")
			logErrWithUri(uri, err.Error(), "warnLevel")
			LogErrorEvent(Cat, "URI.ParseError", "")
			return
		}
	}

	//parse storage from url parameters
	store, storagetype, err1 := FindStorage(params, Cat)
	if err1 != nil {
		err = errors.New("Storage.ParseError")
		logErrWithUri(uri, err1.Error(), "warnLevel")
		LogErrorEvent(Cat, "Storage.ParseError", err1.Error())
		return
	}
	//parse handlers chain from url parameters
	var chain *proc.ProcessorChain = nil
	var buildErr *buildError = nil
	if isDigimarkUrl == true {
		chain, buildErr = handler.ChainBuilder.DigimarkProcChain(params)
	} else {
		chain, buildErr = handler.ChainBuilder.Build(params)
	}
	if buildErr != nil {
		err = errors.New(buildErr.Type())
		logErrWithUri(uri, buildErr.Error(), "warnLevel")
		LogErrorEvent(Cat, buildErr.Type(), buildErr.Error())
		return
	}
	//download image from storage
	var bts []byte
	func() {
		type storageError interface {
			Error() string
			Normal() bool //is normal error?
			Type() string //error type
		}

		var err1 error
		getimagetran := Cat.NewTransaction("Storage", storagetype)
		defer func() {
			if err1 != nil {
				logErrWithUri(uri, err1.Error(), "errorLevel")
				e, ok := err1.(storageError)
				if ok && e.Normal() {
					err = errors.New(fmt.Sprintf("%v.%v", storagetype, e.Type()))
					LogErrorEvent(Cat, fmt.Sprintf("%v.%v", storagetype, e.Type()), e.Error())
				} else {
					err = errors.New(storagetype + ".UnExpectedError")
					LogErrorEvent(Cat, err.Error(), err1.Error())
					isSuccess = false
				}
			} else if len(bts) == 0 {
				err = errors.New(storagetype + ".ImgLenZero")
				LogErrorEvent(Cat, err.Error(), "recv image length is 0")
				logErrWithUri(uri, "recv image length is 0", "warnLevel")
			}
			if isSuccess {
				getimagetran.SetStatus("0")
			} else {
				getimagetran.SetStatus(err)
			}
			getimagetran.Complete()
		}()
		bts, err1 = store.GetImage()
	}()
	if err != nil {
		return
	}
	size := len(bts)
	sizestr := strconv.Itoa(size)
	tran.AddData("size", sizestr)
	Cat.LogEvent("Size", GetImageSizeDistribution(size))

	log.WithFields(log.Fields{
		"size": size,
		"uri":  uri,
	}).Debug("recv image length")
	format, _ := params["ext"]
	img := &img4g.Image{Blob: bts, Format: format, Cat: Cat}

	rspChan := make(chan bool, 1)
	task := &nepheleTask{inImg: img, chain: chain, rspChan: rspChan, CatInstance: Cat, canceled: false}
	taskChan <- task

	select {
	case ok := <-rspChan:
		if !ok {
			err = errors.New("ProcessError")
			isSuccess = false
			logErrWithUri(uri, err.Error(), "errorLevel")
			return
		}
	case <-time.After(time.Second * 5):
		task.SetCanceled()
		err = errors.New("ProcessTimeout")
		logErrWithUri(uri, err.Error(), "errorLevel")
		isSuccess = false
		LogErrorEvent(Cat, "ProcessTimeout", "")
		return
	}

	writer.Header().Set("Content-Type", "image/"+format)
	writer.Header().Set("Content-Length", strconv.Itoa(len(img.Blob)))
	writer.Header().Set("Last-Modified", "2015/1/1 01:01:01")
	log.WithFields(log.Fields{
		"size": size,
		"uri":  uri,
	}).Debug("final image size")
	if _, err1 = writer.Write(img.Blob); err1 != nil {
		logErrWithUri(uri, err1.Error(), "errorLevel")
		err = errors.New("Response.WriteError")
		LogErrorEvent(Cat, "Response.Writeerror", err1.Error())
		isSuccess = false
	}
}