Example #1
0
        public HttpHeaders(SocketReader socketReader)
        {
            int lineNum = 0;

            string line;
            string lastField = null;

            while ("\r\n" != (line = socketReader.NextLine()))
            {
                ++lineNum;

                Match match = headerFieldRegex.Match(line);
                if (match.Success)
                {
                    string name  = match.Groups[1].Value.Trim().ToLower();
                    string value = match.Groups[2].Value;
                    if (!fields.Contains(name))
                    {
                        fields[name] = value;
                    }
                    else
                    {
                        fields[name] = (string)fields[name] + "," + value;
                    }

                    // in case continuation lines follow
                    lastField = name;
                }
                else
                {
                    Match match2 = continuationRegex.Match(line);
                    if (match2.Success)
                    {
                        string value = match2.Groups[1].Value;
                        if (lastField != null)
                        {
                            fields[lastField] = (string)fields[lastField] + value;
                        }
                        else
                        {
                            Trace.Fail("Illegal HTTP header: folding whitespace detected at illegal location (line " + lineNum + ")");
                        }
                    }
                    else
                    {
                        Trace.Fail("Illegal HTTP header: could not parse line " + lineNum);
                    }
                }
            }
        }
        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 HttpHeaders(SocketReader socketReader)
		{
			int lineNum = 0;

			string line;
			string lastField = null;
			while ("\r\n" != (line = socketReader.NextLine()))
			{
				++lineNum;

				Match match = headerFieldRegex.Match(line);
				if (match.Success)
				{
					string name = match.Groups[1].Value.Trim().ToLower();
					string value = match.Groups[2].Value;
					if (!fields.Contains(name))
						fields[name] = value;
					else
						fields[name] = (string)fields[name] + "," + value;

					// in case continuation lines follow
					lastField = name;
				}
				else
				{
					Match match2 = continuationRegex.Match(line);
					if (match2.Success)
					{
						string value = match2.Groups[1].Value;
						if (lastField != null)
						{
							fields[lastField] = (string)fields[lastField] + value;
						}
						else
						{
							Trace.Fail("Illegal HTTP header: folding whitespace detected at illegal location (line " + lineNum + ")");
						}
					}
					else
					{
						Trace.Fail("Illegal HTTP header: could not parse line " + lineNum);
					}
				}
			}
		}
		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;
		}
Example #5
0
        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();
            }
        }
		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();
			}
		}