Coder Social home page Coder Social logo

httpmachine's Introduction

HttpMachine

HttpMachine is a C# HTTP request parser. It implements a state machine with Adrian Thurston's excellent state machine compiler, Ragel. Because Ragel supports C, D, Java, Ruby, it wouldn't be hard to port this library to those languages.

HttpMachine is Copyright (c) 2011 Benjamin van der Veen. HttpMachine is licensed under the MIT License. See LICENSE.txt.

Features

  • HTTP/1.1 and 1.0
  • Supports pipelined requests
  • Tells your server if it should keep-alive
  • Extracts the length of the entity body
  • Support for parsing responses.

Eminently-possible future features

  • Support for decoding chunked transfers.
  • Support for protocol upgrade.

Usage

HttpMachine provides HTTP data through callbacks. To receive these callbacks, implement either the IHttpRequestParserDelegate or the IHttpResponseParserDelegate interface.

// common interface for both requests and responses
public interface IHttpParserDelegate
{
    void OnMessageBegin(HttpParser parser);
    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);
}

public interface IHttpRequestParserDelegate : IHttpParserDelegate
{
    void OnMethod(HttpParser parser, string method);
    void OnRequestUri(HttpParser parser, string requestUri);
    void OnPath(HttpParser parser, string path);
    void OnFragment(HttpParser parser, string fragment);
    void OnQueryString(HttpParser parser, string queryString);
}

public interface IHttpResponseParserDelegate : IHttpParserDelegate
{
    void OnResponseCode(HttpParser parser, int statusCode, string statusReason); 
}

Then, create an instance of HttpParser. Whenever you read data, execute the parser on the data. The Execute method returns the number of bytes successfully parsed. If the returned value is not the same as the length of the buffer you provided, an error occurred while parsing. Make sure you provide a zero-length buffer at the end of the stream, as some callbacks may still be pending.

var handler = new MyHttpParserDelegate();
var parser = new HttpParser(handler);

var buffer = new byte[1024 /* or whatever you like */]

int bytesRead;

while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
    if (bytesRead != parser.Execute(new ArraySegment<byte>(buffer, 0, bytesRead))
        goto error; /* or whatever you like */

// ensure you get the last callbacks.
parser.Execute(default(ArraySegment<byte>));

The parser has three public properties:

// HTTP version provided in the request
public int MajorVersion { get; }
public int MinorVersion { get; }

// inspects "Connection" header and HTTP version (if any) to recommend a connection behavior
public bool ShouldKeepAlive { get; }

These properties are only guaranteed to be accurate in the OnBody and OnMessageEnd callbacks.

httpmachine's People

Contributors

bvanderveen avatar mcunha avatar nrk avatar paulecoyote avatar sergey-litvinov-work avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

httpmachine's Issues

A mechanism to ensure that the parser will only parse requests or only parse responses

As described by @sergey-litvinov on #10:

  • update IHttpParserDelegate just to have common methods between request and response (Headers, Body, etc).
  • create two interfaces IHttpRequestParserDelegate and IHttpResponseParserDelegate that will inherit IHttpParserDelegate with Request\Response specific methods.
  • two constructors for HttpParser.
    • one receives IHttpRequestParserDelegate.
    • one receives IHttpResponseParserDelegate.
  • two member variables
    • one IHttpParserDelegate variable
    • a flag as to whether the delegate is a IHttpRequestParserDelegate or a IHttpResponseParserDelegate
  • in the request/response-specific embedded actions, check the flag on and throw and exception if the value is unexpected

Chunked POST results in parse error

I've been attempting to use Kayak to build a simple event viewer for an event notification service. The client POSTs SOAP messages, but chunks them, rather than setting Content-Length.

So far as I can tell, HttpMachine does not yet support chunked transfer, judging from the comments in the source.

I can however supply WIreshark PCAPs of the offending message, and have created a test case that reliably fails.

Add license.

Could you please license your code under MIT.

Without a license I can't reuse any of your code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.