public override bool Handle(string httpMethod, string path, string querystring, HttpHeaders headers, XmlDocument request, out XmlDocument response) { response = null; try { object result; if (_invoker.Invoke(request, out result)) { response = XmlRpcFormatter.EncodeResponse(result); return true; } if (_final) { string methodName; object[] parameters; XmlRpcParser.Parse(request, out methodName, out parameters); response = XmlRpcFormatter.EncodeFault(405, "XML-RPC method '" + methodName + "' not implemented"); return true; } return false; } catch (XmlRpcServerException xpse) { response = XmlRpcFormatter.EncodeFault(xpse.FaultCode, xpse.FaultString); } catch (Exception e) { response = XmlRpcFormatter.EncodeFault(500, e.Message); } return true; }
public Request(string httpMethod, string path, string querystring, HttpHeaders headers, Stream requestBody) { _httpMethod = httpMethod; _path = path; _querystring = querystring; _headers = headers; _requestBody = requestBody; }
private bool Handler(string method, string uri, HttpHeaders headers, SocketReader reader, Socket socket) { if (_handlers.Count == 0) return false; string path, querystring; int qindex = uri.IndexOf('?'); if (qindex < 0) { path = uri; querystring = null; } else { path = uri.Substring(0, qindex); querystring = qindex == (uri.Length - 1) ? "" : uri.Substring(qindex + 1); } MemoryStream requestBodyStream = new MemoryStream(); byte[] buffer = new byte[8192]; int bytesRead; while (0 != (bytesRead = reader.Read(buffer, 0, buffer.Length))) { requestBodyStream.Write(buffer, 0, bytesRead); } requestBodyStream.Seek(0, SeekOrigin.Begin); Stream readOnlyRequestBodyStream = new ReadOnlyStream(requestBodyStream); readOnlyRequestBodyStream.Seek(0, SeekOrigin.Begin); Request request = new Request(method, path, querystring, headers, readOnlyRequestBodyStream); Response response = new Response(new MemoryStream()); if (StartFilter.Filter(request, response)) { response.Stream.Seek(0, SeekOrigin.Begin); StreamHelper.Transfer(response.Stream, new SocketStream(socket, false)); return true; } return false; }
public abstract bool Handle(string httpMethod, string path, string querystring, HttpHeaders headers, XmlDocument request, out XmlDocument response);
private void HandleConnection(Socket remote) { try { if (localOnly && !((IPEndPoint)remote.RemoteEndPoint).Address.Equals(IPAddress.Loopback)) { Trace.WriteLine("Rejecting non-loopback connection from remote host"); return; } else { SocketReader reader = new SocketReader(remote, 2048); string val = reader.NextLine(); Match match = reqLine.Match(val); if (!match.Success) { Trace.WriteLine("Rejecting malformed HTTP query: " + val); return; } else { HttpHeaders headers = new HttpHeaders(reader); string method = match.Groups[1].Value; string uri = match.Groups[2].Value; string version = match.Groups[3].Value; if (headers.ContentLength >= 0) reader.SetBytesRemaining(headers.ContentLength); else if (string.Compare(method, "GET", true, CultureInfo.InvariantCulture) == 0) reader.SetBytesRemaining(0); // Console.WriteLine("URI: " + uri); OnHandleConnectionBegin(); bool success = false; for (int i = 0; i < handlers.Count; i++) { HandlerRegistration reg = (HandlerRegistration) handlers[i]; if (reg.Pattern.IsMatch(uri)) { if (reg.Handler(method.ToUpper(CultureInfo.InvariantCulture), uri, headers, reader, remote)) { success = true; break; } } } if (!success) HttpHelper.SendErrorCode(remote, 404, "Not found"); return; } } } finally { remote.Shutdown(SocketShutdown.Both); remote.Close(); } }