| 225 | } |
| 226 | |
| 227 | func (h *requestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 228 | path := h.config.NormalizedPath() |
| 229 | if h.httpHandler != nil && !strings.HasPrefix(r.URL.Path, path) { |
| 230 | h.httpHandler.ServeHTTP(w, r) |
| 231 | return |
| 232 | } |
| 233 | |
| 234 | if h.config.Host != "" && !equalHost(r.Host, h.config.Host) { |
| 235 | http.NotFound(w, r) |
| 236 | return |
| 237 | } |
| 238 | |
| 239 | if !strings.HasPrefix(r.URL.Path, path) { |
| 240 | http.NotFound(w, r) |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | h.config.WriteResponseHeader(w, r.Method, r.Header) |
| 245 | length := h.xPaddingBytes.Rand() |
| 246 | config := XPaddingConfig{Length: length} |
| 247 | |
| 248 | if h.config.XPaddingObfsMode { |
| 249 | config.Placement = XPaddingPlacement{ |
| 250 | Placement: h.config.XPaddingPlacement, |
| 251 | Key: h.config.XPaddingKey, |
| 252 | Header: h.config.XPaddingHeader, |
| 253 | } |
| 254 | config.Method = PaddingMethod(h.config.XPaddingMethod) |
| 255 | } else { |
| 256 | config.Placement = XPaddingPlacement{ |
| 257 | Placement: PlacementHeader, |
| 258 | Header: "X-Padding", |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | h.config.ApplyXPaddingToResponse(w, config) |
| 263 | |
| 264 | if r.Method == "OPTIONS" { |
| 265 | w.WriteHeader(http.StatusOK) |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | paddingValue, _ := h.config.ExtractXPaddingFromRequest(r, h.config.XPaddingObfsMode) |
| 270 | if !h.config.IsPaddingValid(paddingValue, h.xPaddingBytes.Min, h.xPaddingBytes.Max, PaddingMethod(h.config.XPaddingMethod)) { |
| 271 | http.Error(w, "invalid xpadding", http.StatusBadRequest) |
| 272 | return |
| 273 | } |
| 274 | sessionId, seqStr := h.config.ExtractMetaFromRequest(r, path) |
| 275 | |
| 276 | var currentSession *httpSession |
| 277 | if sessionId != "" { |
| 278 | currentSession = h.upsertSession(sessionId) |
| 279 | } |
| 280 | |
| 281 | // stream-up upload: POST /path/{session} |
| 282 | if r.Method != http.MethodGet && sessionId != "" && seqStr == "" && h.allowStreamUpUpload() { |
| 283 | httpSC := newHTTPServerConn(w, r.Body) |
| 284 | err := currentSession.uploadQueue.Push(Packet{ |