-1

I use fasthttp for a file server project. The file server has an upload function. For uploading files I pass a key as a URL Query to validate the permission for upload.

The main():

// start http server
log.Printf("Starting HTTP server on %q", listento)
go func() {
    h := &fasthttp.Server{
        Handler: requestHandler,
        MaxRequestBodySize: 2 * 1024 * 1024 * 1024,
    }
    if err := h.ListenAndServe(listento); err != nil {
        log.Panicf("error in ListenAndServe: %s", err)
    }

}()

The requestHandler function:

func requestHandler(ctx *fasthttp.RequestCtx) {
    switch string(ctx.Path()) {
    case "/uploadx":    
        log.Println("Upload: ["+ctx.RemoteIP().String()+"] ["+string(ctx.Path())+"]")
    }   
}

I upload a big file and unfortunately the requestHandler gets triggered when the file upload process is completed. But it should be triggered on start of the upload process, because i want to avoid someone upload a 500MB file without check permission first.

Is there any way to make the requestHandler trigger faster? On the start of the Upload Process?

The Server itself received the first part of the HTTP Request, so the big question is, why does fasthttp trigger requestHandler so late?


I tried now net/http:

mux.HandleFunc("/upload", uploadFile)

func uploadFile(w http.ResponseWriter, r *http.Request) {
    fmt.Println("File Upload Endpoint Hit")
    fmt.Println(r)
}

With net/http i receive the File Upload Endpoint Hit already on start of the FileUpload - exactly like required but i really prefer to use fasthttp.

Am i doing something wrong? Thanks

ChrisG
  • 202
  • 2
  • 13

1 Answers1

1

For big file uploads browsers send a Expect: 100-continue header to ask the server if it's ok for them to continue with the upload. You could use https://godoc.org/github.com/valyala/fasthttp#Server.ContinueHandler to check for permissions and either allow or reject the upload.

Fasthttp will always read the full response before calling a handler. This allows for a more performant API with less allocations.

Erik Dubbelboer
  • 127
  • 1
  • 6
  • The problem is the Browser needs to send a `Expect: 100-continue` otherwise the situation to check the permission of the uploader will never happen - before the full upload is done. Im using a JS XHR to upload the file. I tried it with GB files and there is no 100-continue. Ok i could implement it now and check it. But i dont need to check permission for users which are anyway in the Member area. Its more important to check permissions for user try to attack the server - and they are not logged in. So if those users browsers are not sending the 100-continue they will upload GB of files – ChrisG Jun 05 '20 at 12:46
  • In that case I'm afraid fasthttp isn't a good fit for you. For performance reasons fasthttp will always read the full body into memory (or for file uploads into a file) before calling the handler. This probably isn't going to change any time soon. – Erik Dubbelboer Jun 06 '20 at 13:57
  • Yes, this was my also conclusion, thats why i moved to `net/http`. `fasthttp` is a great package and more comfortable to use, thats why i asked this question and was in hope that there is some way. – ChrisG Jun 06 '20 at 15:35