private void AcceptClient(IAsyncResult ar) { try { using (HTTPServerSession session = new HTTPServerSession(_socket.EndAccept(ar), _params)) { ManualResetEvent evt = ar.AsyncState as ManualResetEvent; evt.Set(); while (session.HasMoreRequests) { try { HTTPServerResponse response = new HTTPServerResponse(session); HTTPServerRequest request = new HTTPServerRequest(session); response.Version = request.Version; response.KeepAlive = session.CanKeepAlive && request.KeepAlive && _params.KeepAlive; try { IHTTPRequestHandler handler = _factory.CreateRequestHandler(request); if (handler != null) { if (request.ExpectsContinue) { response.SendContinue(); } handler.HandleRequest(request, response); session.KeepAlive = response.KeepAlive && session.CanKeepAlive && _params.KeepAlive; } else { SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_NOT_IMPLEMENTED); } } catch (Exception ex) { if (!response.Sent) { SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_INTERNAL_SERVER_ERROR); } OnServerException(ex); break; } } catch (HTTPNoMessageException) { break; } catch (HTTPMessageException) { SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_BAD_REQUEST); break; } } } } catch (Exception) { } }
/// <summary> /// 实现Request响应 /// </summary> /// <param name="request"></param> /// <param name="response"></param> public void HandleRequest(HTTPServerRequest request, HTTPServerResponse response) { /** * In this example we'll write the body into the * stream obtained by response.Send(). This will cause the * KeepAlive to be false since the size of the body is not * known when the response header is sent. **/ if (request.URI.ToLower() == "/favicon.ico") { response.ContentType = "text/html;charset=utf-8"; response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_NOT_FOUND; response.SendContinue(); } else if (request.URI.ToLower() == "/api") { response.ContentType = "application/json;charset=utf-8"; SendResponse(response, caller.GetAPIText()); } else if (request.URI.ToLower() == "/help") { //发送文档帮助信息 response.ContentType = "text/html;charset=utf-8"; response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_OK; SendResponse(response, caller.GetDocument(null)); } else if (request.URI.ToLower().IndexOf("/help/") == 0) { //发送文档帮助信息 response.ContentType = "text/html;charset=utf-8"; response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_OK; var name = request.URI.Substring(request.URI.IndexOf("/help/") + 6); SendResponse(response, caller.GetDocument(name)); } else if (request.URI.Substring(request.URI.IndexOf('/') + 1).Length > 5) { HandleResponse(request, response); } else { response.StatusAndReason = HTTPServerResponse.HTTPStatus.HTTP_NOT_ACCEPTABLE; var error = new HttpServiceResult { Message = response.Reason }; SendResponse(response, error); } }
private void AcceptClient(IAsyncResult ar) { try { using (HTTPServerSession session = new HTTPServerSession(_socket.EndAccept(ar), _params)) { ManualResetEvent evt = ar.AsyncState as ManualResetEvent; evt.Set(); while (session.HasMoreRequests) { try { HTTPServerResponse response = new HTTPServerResponse(session); HTTPServerRequest request = new HTTPServerRequest(session); response.Version = request.Version; response.KeepAlive = session.CanKeepAlive && request.KeepAlive && _params.KeepAlive; try { IHTTPRequestHandler handler = _factory.CreateRequestHandler(request); if (handler != null) { if (request.ExpectsContinue) response.SendContinue(); handler.HandleRequest(request, response); session.KeepAlive = response.KeepAlive && session.CanKeepAlive && _params.KeepAlive; } else SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_NOT_IMPLEMENTED); } catch (Exception ex) { if (!response.Sent) SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_INTERNAL_SERVER_ERROR); OnServerException(ex); break; } } catch (HTTPNoMessageException) { break; } catch (HTTPMessageException) { SendErrorResponse(session, HTTPServerResponse.HTTPStatus.HTTP_BAD_REQUEST); break; } } } } catch (Exception) { } }