protected HttpRequestParser(HttpClient client, int contentLength) { if (client == null) throw new ArgumentNullException("client"); Client = client; ContentLength = contentLength; }
public HttpMultiPartRequestParser(HttpClient client, int contentLength, string boundary) : base(client, contentLength) { if (boundary == null) throw new ArgumentNullException("boundary"); _firstBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); _separatorBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary); Client.MultiPartItems = new List<HttpMultiPartItem>(); }
internal HttpRequest(HttpClient client) { ParseHeaders(client); Form = client.PostParameters; ParseMultiPartItems(client); HttpMethod = RequestType = client.Method; ParsePath(client); ParseRemoteEndPoint(client); BuildServerVariables(client); BuildParams(); InputStream = client.InputStream; if (InputStream != null) InputStream.Position = 0; }
private void RegisterClient(HttpClient client) { if (client == null) throw new ArgumentNullException("client"); lock (_syncLock) { _clients.Add(client, true); _clientsChangedEvent.Set(); } }
private void AcceptTcpClientCallback(IAsyncResult asyncResult) { if (_state != HttpServerState.Started) return; try { var listener = _listener; // Prevent race condition. if (listener == null) return; var tcpClient = listener.EndAcceptTcpClient(asyncResult); var client = new HttpClient(this, tcpClient); RegisterClient(client); client.BeginRequest(); BeginAcceptTcpClient(); } catch (ObjectDisposedException) { // EndAcceptTcpClient will throw a ObjectDisposedException // when we're shutting down. This can safely be ignored. } catch (Exception ex) { Log.Info("Failed to accept TCP client", ex); } }
internal void UnregisterClient(HttpClient client) { if (client == null) throw new ArgumentNullException("client"); lock (_syncLock) { Debug.Assert(_clients.ContainsKey(client)); _clients.Remove(client); _clientsChangedEvent.Set(); } }
internal HttpContext(HttpClient client) { Server = client.Server.ServerUtility; Request = new HttpRequest(client); Response = new HttpResponse(this); }
public HttpUrlEncodedRequestParser(HttpClient client, int contentLength) : base(client, contentLength) { _stream = new MemoryStream(); }
private void ParseHeaders(HttpClient client) { Headers = CreateCollection(client.Headers); string header; // Parse Accept. if (client.Headers.TryGetValue("Accept", out header)) { string[] parts = header.Split(','); HttpUtil.TrimAll(parts); AcceptTypes = parts; } else { AcceptTypes = EmptyStringArray; } // Parse Content-Type. if (client.Headers.TryGetValue("Content-Type", out header)) { string[] parts = header.Split(new[] { ';' }, 2); ContentType = parts[0].Trim(); if (parts.Length == 2) { string[] encoding = parts[1].Trim().Split(new[] { '=' }, 2); if (encoding.Length == 2 && String.Equals(encoding[0], "charset", StringComparison.OrdinalIgnoreCase)) { ContentEncoding = Encoding.GetEncoding(encoding[1]); } } } // Parse Content-Length. if (client.Headers.TryGetValue("Content-Length", out header)) { int contentLength; if (int.TryParse(header, out contentLength)) ContentLength = contentLength; } // Parse Referer. if (client.Headers.TryGetValue("Referer", out header)) UrlReferer = new Uri(header); // Parse User-Agent. if (client.Headers.TryGetValue("User-Agent", out header)) UserAgent = header; // Parse Accept-Language. if (client.Headers.TryGetValue("Accept-Language", out header)) { string[] parts = header.Split(','); HttpUtil.TrimAll(parts); UserLanguages = parts; } else { UserLanguages = EmptyStringArray; } // Parse Cookie. Cookies = new HttpCookieCollection(); if (client.Headers.TryGetValue("Cookie", out header)) { string[] parts = header.Split(';'); foreach (string part in parts) { string[] partParts = part.Split(new[] { '=' }, 2); string name = partParts[0].Trim(); string value = partParts.Length == 1 ? null : partParts[1]; Cookies.AddCookie(new HttpCookie(name, value), true); } } }
private void BuildServerVariables(HttpClient client) { ServerVariables = new NameValueCollection(); // Add all headers. var allHttp = new StringBuilder(); var allRaw = new StringBuilder(); foreach (var item in client.Headers) { ServerVariables[item.Key] = item.Value; string httpKey = "HTTP_" + (item.Key.Replace('-', '_')).ToUpperInvariant(); ServerVariables[httpKey] = item.Value; allHttp.Append(httpKey); allHttp.Append('='); allHttp.Append(item.Value); allHttp.Append("\r\n"); allRaw.Append(item.Key); allRaw.Append('='); allRaw.Append(item.Value); allRaw.Append("\r\n"); } ServerVariables["ALL_HTTP"] = allHttp.ToString(); ServerVariables["ALL_RAW"] = allRaw.ToString(); ServerVariables["CONTENT_LENGTH"] = ContentLength.ToString(CultureInfo.InvariantCulture); ServerVariables["CONTENT_TYPE"] = ContentType; ServerVariables["LOCAL_ADDR"] = client.Server.EndPoint.Address.ToString(); ServerVariables["PATH_INFO"] = Path; string[] parts = client.Request.Split(new[] { '?' }, 2); ServerVariables["QUERY_STRING"] = parts.Length == 2 ? parts[1] : ""; ServerVariables["REMOTE_ADDR"] = UserHostAddress; ServerVariables["REMOTE_HOST"] = UserHostName; ServerVariables["REMOTE_PORT"] = null; ServerVariables["REQUEST_METHOD"] = RequestType; ServerVariables["SCRIPT_NAME"] = Path; ServerVariables["SERVER_NAME"] = client.Server.ServerUtility.MachineName; ServerVariables["SERVER_PORT"] = client.Server.EndPoint.Port.ToString(CultureInfo.InvariantCulture); ServerVariables["SERVER_PROTOCOL"] = client.Protocol; ServerVariables["URL"] = Path; }
private void ParseRemoteEndPoint(HttpClient client) { var endPoint = (IPEndPoint)client.TcpClient.Client.RemoteEndPoint; UserHostName = UserHostAddress = endPoint.Address.ToString(); }
private void ParsePath(HttpClient client) { RawUrl = client.Request; string[] parts = client.Request.Split(new[] { '?' }, 2); Path = parts[0]; QueryString = new NameValueCollection(); if (parts.Length == 2) HttpUtil.UrlDecodeTo(parts[1], QueryString); string host; string port; string hostHeader; if (client.Headers.TryGetValue("Host", out hostHeader)) { parts = hostHeader.Split(new[] { ':' }, 2); host = parts[0]; if (parts.Length == 2) port = parts[1]; else port = null; } else { var endPoint = client.Server.EndPoint; host = endPoint.Address.ToString(); if (endPoint.Port == 80) port = null; else port = endPoint.Port.ToString(CultureInfo.InvariantCulture); } var sb = new StringBuilder(); sb.Append("http://"); sb.Append(host); if (port != null) { sb.Append(':'); sb.Append(port); } sb.Append(client.Request); Url = new Uri(sb.ToString()); }
private void ParseMultiPartItems(HttpClient client) { Files = new HttpFileCollection(); if (client.MultiPartItems == null) return; foreach (var item in client.MultiPartItems) { string contentType = null; string name = null; string fileName = null; string header; if (item.Headers.TryGetValue("Content-Disposition", out header)) { string[] parts = header.Split(';'); for (int i = 0; i < parts.Length; i++) { string part = parts[i].Trim(); if (part.StartsWith("name=")) name = ParseContentDispositionItem(part.Substring(5)); else if (part.StartsWith("filename=")) fileName = ParseContentDispositionItem(part.Substring(9)); } } if (item.Headers.TryGetValue("Content-Type", out header)) contentType = header; if (name == null) { Log.Info("Received multipart item without name"); continue; } if (item.Value != null) { Form[name] = item.Value; } else { Files.AddFile(name, new HttpPostedFile((int)item.Stream.Length, contentType, fileName, item.Stream)); } } }
public HttpUnknownRequestParser(HttpClient client, int contentLength) : base(client, contentLength) { Client.InputStream = new MemoryStream(); }