@@ -102,14 +102,22 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
102102
103103 h .config .WriteResponseHeader (writer )
104104
105+ validRange := h .config .GetNormalizedXPaddingBytes ()
106+ x_padding := int32 (len (request .URL .Query ().Get ("x_padding" )))
107+ if validRange .To > 0 && (x_padding < validRange .From || x_padding > validRange .To ) {
108+ errors .LogInfo (context .Background (), "invalid x_padding length:" , x_padding )
109+ writer .WriteHeader (http .StatusBadRequest )
110+ return
111+ }
112+
105113 sessionId := ""
106114 subpath := strings .Split (request .URL .Path [len (h .path ):], "/" )
107115 if len (subpath ) > 0 {
108116 sessionId = subpath [0 ]
109117 }
110118
111- if sessionId == "" {
112- errors .LogInfo (context .Background (), "no sessionid on request:" , request . URL . Path )
119+ if sessionId == "" && h . config . Mode != "" && h . config . Mode != "auto" && h . config . Mode != "stream-one" && h . config . Mode != "stream-up" {
120+ errors .LogInfo (context .Background (), "stream-one mode is not allowed" )
113121 writer .WriteHeader (http .StatusBadRequest )
114122 return
115123 }
@@ -126,17 +134,20 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
126134 }
127135 }
128136
129- currentSession := h .upsertSession (sessionId )
137+ var currentSession * httpSession
138+ if sessionId != "" {
139+ currentSession = h .upsertSession (sessionId )
140+ }
130141 scMaxEachPostBytes := int (h .ln .config .GetNormalizedScMaxEachPostBytes ().To )
131142
132- if request .Method == "POST" {
143+ if request .Method == "POST" && sessionId != "" {
133144 seq := ""
134145 if len (subpath ) > 1 {
135146 seq = subpath [1 ]
136147 }
137148
138149 if seq == "" {
139- if h .config .Mode = = "packet -up" {
150+ if h .config .Mode != "" && h . config . Mode ! = "auto" && h . config . Mode != "stream -up" {
140151 errors .LogInfo (context .Background (), "stream-up mode is not allowed" )
141152 writer .WriteHeader (http .StatusBadRequest )
142153 return
@@ -148,13 +159,16 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
148159 errors .LogInfoInner (context .Background (), err , "failed to upload (PushReader)" )
149160 writer .WriteHeader (http .StatusConflict )
150161 } else {
162+ if request .Header .Get ("Content-Type" ) == "application/grpc" {
163+ writer .Header ().Set ("Content-Type" , "application/grpc" )
164+ }
151165 writer .WriteHeader (http .StatusOK )
152166 <- request .Context ().Done ()
153167 }
154168 return
155169 }
156170
157- if h .config .Mode = = "stream -up" {
171+ if h .config .Mode != "" && h . config . Mode ! = "auto" && h . config . Mode != "packet -up" {
158172 errors .LogInfo (context .Background (), "packet-up mode is not allowed" )
159173 writer .WriteHeader (http .StatusBadRequest )
160174 return
@@ -193,24 +207,29 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
193207 }
194208
195209 writer .WriteHeader (http .StatusOK )
196- } else if request .Method == "GET" {
210+ } else if request .Method == "GET" || sessionId == "" {
197211 responseFlusher , ok := writer .(http.Flusher )
198212 if ! ok {
199213 panic ("expected http.ResponseWriter to be an http.Flusher" )
200214 }
201215
202- // after GET is done, the connection is finished. disable automatic
203- // session reaping, and handle it in defer
204- currentSession .isFullyConnected .Close ()
205- defer h .sessions .Delete (sessionId )
216+ if sessionId != "" {
217+ // after GET is done, the connection is finished. disable automatic
218+ // session reaping, and handle it in defer
219+ currentSession .isFullyConnected .Close ()
220+ defer h .sessions .Delete (sessionId )
221+ }
206222
207223 // magic header instructs nginx + apache to not buffer response body
208224 writer .Header ().Set ("X-Accel-Buffering" , "no" )
209225 // A web-compliant header telling all middleboxes to disable caching.
210226 // Should be able to prevent overloading the cache, or stop CDNs from
211227 // teeing the response stream into their cache, causing slowdowns.
212228 writer .Header ().Set ("Cache-Control" , "no-store" )
213- if ! h .config .NoSSEHeader {
229+
230+ if request .Header .Get ("Content-Type" ) == "application/grpc" {
231+ writer .Header ().Set ("Content-Type" , "application/grpc" )
232+ } else if ! h .config .NoSSEHeader {
214233 // magic header to make the HTTP middle box consider this as SSE to disable buffer
215234 writer .Header ().Set ("Content-Type" , "text/event-stream" )
216235 }
@@ -227,9 +246,12 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
227246 downloadDone : downloadDone ,
228247 responseFlusher : responseFlusher ,
229248 },
230- reader : currentSession . uploadQueue ,
249+ reader : request . Body ,
231250 remoteAddr : remoteAddr ,
232251 }
252+ if sessionId != "" {
253+ conn .reader = currentSession .uploadQueue
254+ }
233255
234256 h .ln .addConn (stat .Connection (& conn ))
235257
0 commit comments