Git Product home page Git Product logo

http-parser.d's Introduction

http-parser.D

http-parser.D = joyent/http-parser in D programming language.

Usage

import std.stdio;
import http.parser.core;
...
auto parser = new HttpParser();
parser.onMessageBegin = (parser) {
	writeln("Message has just begun");
};
parser.onMessageComplete = (parser) {
	writeln("Message has been completed");
};
parser.onUrl = (parser, string data) {
	writeln("Url of HTTP message is: ", data);
};
parser.onStatusComplete = (parser) {
	writeln("HTTP status is complete");
    // if parsing request, this is a good spot to query for:
    if(parser.type == HttpParserType.REQUEST) {
        string method = parser.method; // GET
        writeln("Method: ", method);
    }
    auto version = parser.protocolVersion.toString; // 1.1
};
parser.onHeader = (parser, HttpHeader header) {
	writeln("Parser Header '", header.name, "' with value '", header.value, "'");
};
parser.onBody = (parser, HttpBodyChunk chunk) {
    string isFinal = chunk.isFinal ? "final" : "not final" ;
	writeln(std.string.format("A chunk of the HTTP body has been processed: %s (%s) ", chunk.buffer, isFinal));
};
parser.execute("GET / HTTP 1.1\r");
parser.execute("\n");
parser.execute("FirstHeader: ValueOfFirst Header\r\n");
parser.execute("Content-Length: 3\r\n");
parser.execute("\r\n");
ubyte[] bodyChunk = [1u,2u,3u];
parser.execute(bodyChunk);

Output:

Message has just begun
Url of HTTP message is: /
Parser Header 'FirstHeader' with value 'ValueOfFirst Header'
Parser Header 'Content-Length' with value '3'
A chunk of the HTTP bady has been processed: [1, 2, 3] (is final)
Message has been completed

Uri

Uri parsing is provided by the class http.parser.Uri.

import http.parser : Uri;

//...

Uri uri = new Uri("http://user1:[email protected]:9000/myResource?indent=1#page_2");
string schema = uri.schema; // -> "http"
string credentials = uri.userInfo; // -> "user1:password1"
string host = uri.host; // -> "google.com"
ushort port = uri.port; // -> 9000
string path = uri.path; // -> "/myResource"
string query = uri.query; // -> "ident=1"
string fragment = uri.fragment; // -> "page_2"

Body Transmission Modes

Once the headers were parsed, HttpParser.transmissionMode can be used to determinate how the body of the message will be transmitted. The values are and will be detected in the following order:

  • HttpBodyTransmissionMode.Chunked: Transfer-Encoding header was found in the request.
  • HttpBodyTransmissionMode.ContentLength: Content-Length > 0 header was found in the request.
  • HttpBodyTransmissionModel.None: No entity expected in the incoming HTTP message.

Building

git clone https://github.com/bithavoc/http-parser.d.git
git submodule update --init
make

An archive will be generated in out/http-parser.a containing joyent/http-parser objects and the http-parser.d object itself.

Examples

Use make examples to compile all the examples. Executables will be generated in out/examples.

Test

make test

License (MIT)

Copyright (c) 2012, 2014 Bithavoc - http://bithavoc.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

http-parser.d's People

Contributors

truedat101 avatar bithavoc avatar darredevil avatar mingodad avatar johanfirebase avatar

Stargazers

Mikhail Mitkevich avatar ttldtor avatar  avatar Sebastian Wilzbach avatar Yannick Koechlin avatar Виталий Дмитриев avatar Oscar Rendon avatar  avatar Roberto Miranda avatar Guillermo Iguaran avatar

Watchers

 avatar Guillermo Iguaran avatar  avatar Roberto Miranda avatar James Cloos avatar

http-parser.d's Issues

can't build http-parser

http://www.digitalmars.com/ctg/optlink.html
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_get_http_parser_data
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _http_parser_init
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_set_http_parser_data
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_alloc_http_parser
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_parser_get_errno
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_errno_description
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_errno_name
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _http_parser_execute
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_minor
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_major
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_method_str
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _http_parser_get_content_length
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_http_status_code
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _http_body_is_final
C:\Users\Dima\AppData\Roaming\dub\packages\http-parser-master\http-parser.lib(co
re)
Error 42: Symbol Undefined _duv_free_http_parser
--- errorlevel 15
FAIL .dub\build\application-debug-windows-x86-dmd_2066-7A5021B283A1A856E27DC0FAB
E3C053D\ seismodownloader executable
Error executing command run: dmd failed with exit code 15.

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.