XHTTP: Add "stream-up" mode for client & server (#3994)

This commit is contained in:
RPRX 2024-11-09 11:05:41 +00:00 committed by GitHub
parent 94c02f090e
commit bc4bf3d38f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 164 additions and 77 deletions

View file

@ -100,6 +100,8 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
return
}
h.config.WriteResponseHeader(writer)
sessionId := ""
subpath := strings.Split(request.URL.Path[len(h.path):], "/")
if len(subpath) > 0 {
@ -134,7 +136,26 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
if seq == "" {
errors.LogInfo(context.Background(), "no seq on request:", request.URL.Path)
if h.config.Mode == "packet-up" {
errors.LogInfo(context.Background(), "stream-up mode is not allowed")
writer.WriteHeader(http.StatusBadRequest)
return
}
err = currentSession.uploadQueue.Push(Packet{
Reader: request.Body,
})
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to upload (PushReader)")
writer.WriteHeader(http.StatusConflict)
} else {
writer.WriteHeader(http.StatusOK)
<-request.Context().Done()
}
return
}
if h.config.Mode == "stream-up" {
errors.LogInfo(context.Background(), "packet-up mode is not allowed")
writer.WriteHeader(http.StatusBadRequest)
return
}
@ -148,14 +169,14 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to upload")
errors.LogInfoInner(context.Background(), err, "failed to upload (ReadAll)")
writer.WriteHeader(http.StatusInternalServerError)
return
}
seqInt, err := strconv.ParseUint(seq, 10, 64)
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to upload")
errors.LogInfoInner(context.Background(), err, "failed to upload (ParseUint)")
writer.WriteHeader(http.StatusInternalServerError)
return
}
@ -166,12 +187,11 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
})
if err != nil {
errors.LogInfoInner(context.Background(), err, "failed to upload")
errors.LogInfoInner(context.Background(), err, "failed to upload (PushPayload)")
writer.WriteHeader(http.StatusInternalServerError)
return
}
h.config.WriteResponseHeader(writer)
writer.WriteHeader(http.StatusOK)
} else if request.Method == "GET" {
responseFlusher, ok := writer.(http.Flusher)
@ -195,8 +215,6 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
writer.Header().Set("Content-Type", "text/event-stream")
}
h.config.WriteResponseHeader(writer)
writer.WriteHeader(http.StatusOK)
responseFlusher.Flush()
@ -223,6 +241,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
conn.Close()
} else {
errors.LogInfo(context.Background(), "unsupported method: ", request.Method)
writer.WriteHeader(http.StatusMethodNotAllowed)
}
}