/// <summary> /// Remove a request handler /// </summary> /// <param name="handler">Request handler to remove</param> public void RemoveHandler(HttpRequestHandler handler) { HttpRequestHandler[] newHandlers = new HttpRequestHandler[_requestHandlers.Length - 1]; try { int j = 0; for (int i = 0; i < _requestHandlers.Length; i++) { if (!_requestHandlers[i].Signature.ExactlyEquals(handler.Signature)) { newHandlers[j++] = handler; } } // CLR guarantees this is an atomic operation _requestHandlers = newHandlers; } catch (IndexOutOfRangeException) { } }
void ProcessRequest(IHttpClientContext context, IHttpRequest request) { LogWriter.Write(this, LogPrio.Trace, "Processing request..."); IHttpResponse response = request.CreateResponse(context); try { // load cookies if they exist. RequestCookies cookies = request.Headers["cookie"] != null ? new RequestCookies(request.Headers["cookie"]) : new RequestCookies(string.Empty); request.SetCookies(cookies); // Create a request signature HttpRequestSignature signature = new HttpRequestSignature(request); // Look for a signature match in our handlers bool found = false; for (int i = 0; i < _requestHandlers.Length; i++) { HttpRequestHandler handler = _requestHandlers[i]; if (signature == handler.Signature) { FireRequestCallback(context, request, response, handler.Callback); found = true; break; } } if (!found) { // No registered handler matched this request's signature if (_notFoundHandler != null) { FireRequestCallback(context, request, response, _notFoundHandler); } else { // Send a default 404 response try { response.Status = HttpStatusCode.NotFound; response.Reason = String.Format("No request handler registered for Method=\"{0}\", Content-Type=\"{1}\", Path=\"{2}\"", signature.Method, signature.ContentType, signature.Path); string notFoundResponse = "<html><head><title>Page Not Found</title></head><body><h3>" + response.Reason + "</h3></body></html>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(notFoundResponse); response.Body.Write(buffer, 0, buffer.Length); response.Send(); } catch (Exception) { } } } } catch (Exception err) { ThrowException(err); bool errorResponse = true; Exception e = err; while (e != null) { if (e is SocketException) { errorResponse = false; break; } e = e.InnerException; } if (errorResponse) { try { #if DEBUG context.Respond(HttpHelper.HTTP11, HttpStatusCode.InternalServerError, "Internal server error", err.ToString(), "text/plain"); #else context.Respond(HttpHelper.HTTP10, HttpStatusCode.InternalServerError, "Internal server error"); #endif } catch (Exception err2) { LogWriter.Write(this, LogPrio.Fatal, "Failed to respond on message with Internal Server Error: " + err2); } } } request.Clear(); LogWriter.Write(this, LogPrio.Trace, "...done processing request."); }
private Object[] handleMultipartFormData() { Object[] rdatas = null; int content_len = 0; MemoryStream ms = new MemoryStream(); if (this.httpHeaders.ContainsKey("Content-Length")) { content_len = Convert.ToInt32(this.httpHeaders["Content-Length"]); if (content_len > MAX_POST_SIZE) { logger.Warn(String.Format("POST Content-Length({0}) too big for this simple server", content_len)); throw new Exception( String.Format("POST Content-Length({0}) too big for this simple server", content_len)); } // read image byte[] buf = new byte[BUF_SIZE]; int to_read = content_len; while (to_read > 0) { //Tool.logDebug("start inputStream.Read"); try { int numread = this.inputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read)); if (numread == 0) { if (to_read == 0) { break; } else { logger.Warn("client disconnected during post"); throw new Exception("Client disconnected during post"); } } to_read -= numread; ms.Write(buf, 0, numread); } catch (Exception e) { logger.Warn(e); content_len = 0; break; } } ms.Seek(0, SeekOrigin.Begin); } if (content_len == 0) { logger.Warn("Content-Length is 0"); HttpResponse.writeFailure(outputStream); } else { HttpRequestHandler handler = new HttpRequestHandler(); rdatas = (Object[])handler.handle(httpHeaders, ms); if (rdatas == null || rdatas.Length == 0) { HttpResponse.writeSuccess(outputStream, "text/html"); } else { HttpResponse.writeSuccess(outputStream, rdatas, "text/html"); } } ms.Dispose(); ms.Close(); ms = null; return(rdatas); }