Esempio n. 1
0
/*
 * Handle HTTPS requests.
 * @param resp http.ResponseWriter - response buffer
 * @param req *http.Request - request data
 */
func handler(resp http.ResponseWriter, req *http.Request) {

	// get requested resource reference
	ref := req.URL.String()
	mth := req.Method
	logger.Println(logger.DBG_ALL, "[https] "+mth+" "+ref)

	//-----------------------------------------------------------------
	// check for POST request (document upload)
	//-----------------------------------------------------------------
	if mth == "POST" {
		// get upload data
		rdr, _, err := req.FormFile("file")
		if err != nil {
			logger.Println(logger.INFO, "[https] Error accessing uploaded file: "+err.Error())
			// show error page
			ref = "/upload_err.html"
			mth = "GET"
		} else {
			content := make([]byte, 0)
			if err = sid.ProcessStream(rdr, 4096, func(data []byte) bool {
				content = append(content, data...)
				return true
			}); err != nil {
				logger.Println(logger.INFO, "[https] Error accessing uploaded file: "+err.Error())
				// show error page
				ref = "/upload_err.html"
				mth = "GET"
			} else {
				// post-process uploaded document
				sid.PostprocessUploadData(content)
				// set resource ref to response page
				ref = "/upload_resp.html"
				mth = "GET"
			}
		}
	}

	//-----------------------------------------------------------------
	// handle GET requests
	//-----------------------------------------------------------------
	if mth == "GET" {
		// set default page
		if ref == "/" {
			ref = "/index.html"
		}
		// handle resource file
		switch {
		case strings.HasSuffix(ref, ".html"):
			resp.Header().Set("Content-Type", "text/html")
		}
		if err := sid.ProcessFile("./www"+ref, 4096, func(data []byte) bool {
			// append data to response buffer
			resp.Write(data)
			return true
		}); err != nil {
			logger.Println(logger.ERROR, "[https] Resource failure: "+err.Error())
		}
	}
}
Esempio n. 2
0
/*
 * Handle (HTML) resource request with special cases like "upload".
 * @param c *sid.Cover - instance reference
 * @param s *sid.State - reference to cover state
 * @return body string - HTML page body
 * @return id string - identifier for cover content (or "")
 */
func HandleRequest(c *sid.Cover, s *sid.State) (body string, id string) {

	//=================================================================
	// Handle upload request on root page
	//=================================================================
	if s.ReqResource == "/" {
		// create boundary identifier and load next image
		delim := sid.CreateId(28)
		img := GetNextImage()

		// create uploadable content
		content := make([]byte, 0)
		if err := sid.ProcessFile(img.path, 4096, func(data []byte) bool {
			content = append(content, data...)
			return true
		}); err != nil {
			logger.Println(logger.ERROR, "[cover] Failed to open upload file: "+img.path)
			return "", ""
		}

		// build POST content suitable for upload to cover site
		// and save it in the handler structure
		lb := "\r\n"
		lb2 := lb + lb
		lb3 := lb2 + lb
		sep := "-----------------------------" + delim
		post :=
			sep + lb + "Content-Disposition: form-data; name=\"imgUrl\"" + lb3 +
				sep + lb + "Content-Disposition: form-data; name=\"fileName[]\"" + lb3 +
				sep + lb + "Content-Disposition: form-data; name=\"file[]\"; filename=\"" +
				img.name + "\"" + lb + "Content-Type: " + img.mime + lb2 + string(content) + lb +
				sep + lb + "Content-Disposition: form-data; name=\"alt[]\"\n\n" + img.comment + lb +
				sep + lb + "Content-Disposition: form-data; name=\"new_width[]\"" + lb3 +
				sep + lb + "Content-Disposition: form-data; name=\"new_height[]\"" + lb3 +
				sep + lb + "Content-Disposition: form-data; name=\"submit\"" + lb2 + "Upload" + lb +
				sep + "--" + lb2
		c.Posts[delim] = []byte(post)

		// assemble upload form
		action := "/" + delim + "/upload"
		total := len(c.Posts[delim]) + 32

		return "<h1>Upload your document:</h1>\n" +
			"<script type=\"text/javascript\">\n" +
			"function a(){" +
			"b=document.u.file.files.item(0).getAsDataURL();" +
			"e=document.u.file.value.length;" +
			"c=Math.ceil(3*(b.substring(b.indexOf(\",\")+1).length+3)/4);" +
			"f=" + strconv.Itoa(total) + "-c-e-307;" +
			"if(f<0){alert(\"File size exceeds limit - can't upload!!\");}else{" +
			"d=\"\";for(i=0;i<f;i++){d+=b.charAt(i%c)}" +
			"document.u.rnd.value=d;" +
			"document.u.submit();" +
			"}}\n" +
			"document.write(\"" +
			"<form enctype=\\\"multipart/form-data\\\" action=\\\"" + action + "\\\" method=\\\"post\\\" name=\\\"u\\\">" +
			"<p><input type=\\\"file\\\" name=\\\"file\\\"/></p>" +
			"<p><input type=\\\"button\\\" value=\\\"Upload\\\" onclick=\\\"a()\\\"/></p>" +
			"<input type=\\\"hidden\\\" name=\\\"rnd\\\" value=\\\"\\\"/>" +
			"</form>\");\n" +
			"</script>" +
			"<noscript><hr/><p><font color=\"red\"><b>" +
			"Uploading files requires JavaScript enabled! Please change the settings " +
			"of your browser and try again...</b></font></p><hr/>" +
			"</noscript>\n" +
			"<hr/>\n", delim
	}

	//=================================================================
	//	Successful upload
	//=================================================================
	if s.ReqResource == "/thumbnail" {
		logger.Println(logger.INFO, "[cover] Successful upload detected!")
		return "<h1>Upload was successful!</h1>", ""
	}

	//=================================================================
	//	Error during upload
	//=================================================================
	logger.Println(logger.INFO, "[cover] Failed upload detected!")
	return "<h1>Upload was NOT successful -- please retry later!</h1>", ""
}