5

Possible Duplicate:
Converting Raw HTTP Request into HTTPWebRequest Object

I've got a custom HTTP server written in C# which gives me the raw HTTP request...

GET /ACTION=TEST HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

Is there something in the .NET framework that I can use to parse it or do I have to do it by hand?

Cheers

1
  • Do you mean you wish to convert a stream of bytes into a HttpRequest instance? I've never come across it, but I imagine there must be some code in the framework that does it (inside HttpWebRequest somewhere perhaps).
    – samjudson
    Jun 10, 2009 at 12:42

2 Answers 2

6

Check out HttpMachine - a component of the Kayak HTTP server for dotNET. HttpMachine is a callback driven HTTP parser.

To wet your appetite, here's the IHttpParserHandler interface:

using System
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HttpMachine
{
    public interface IHttpParserHandler
    {
        void OnMessageBegin(HttpParser parser);
        void OnMethod(HttpParser parser, string method);
        void OnRequestUri(HttpParser parser, string requestUri);
        void OnFragment(HttpParser parser, string fragment);
        void OnQueryString(HttpParser parser, string queryString);
        void OnHeaderName(HttpParser parser, string name);
        void OnHeaderValue(HttpParser parser, string value);
        void OnHeadersEnd(HttpParser parser);
        void OnBody(HttpParser parser, ArraySegment<byte> data);
        void OnMessageEnd(HttpParser parser);
    }
}
1
  • I implemented this in a transparent proxy of mine. When it works, it works great. But when it doesn't, it dies a terrible death. One misplaced character and it's dead. Great, but does not fail gracefully.
    – user562566
    Dec 22, 2014 at 9:03
5

Looks like this question has been asked before here. Apparently there is no built-in way to do it.

1
  • ah, you beat me to it. thanks :-) Jun 10, 2009 at 12:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.