Coder Social home page Coder Social logo

httpparser's Introduction

httpparser

Simple and fast HTTP request, response and urls parser written in C++.

Examples

#include <iostream>

#include <httpparser/request.h>
#include <httpparser/httprequestparser.h>

using namespace httpparser;

int main(int, char**)
{
    const char text[] = "GET /uri.cgi HTTP/1.1\r\n"
                        "User-Agent: Mozilla/5.0\r\n"
                        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
                        "Host: 127.0.0.1\r\n"
                        "\r\n";

    Request request;
    HttpRequestParser parser;

    HttpRequestParser::ParseResult res = parser.parse(request, text, text + strlen(text));

    if( res == HttpRequestParser::ParsingCompleted )
    {
        std::cout << request.inspect() << std::endl;
        return EXIT_SUCCESS;
    }
    else
    {
        std::cerr << "Parsing failed" << std::endl;
        return EXIT_FAILURE;
    }
}
#include <iostream>

#include <httpparser/response.h>
#include <httpparser/httpresponseparser.h>

using namespace httpparser;

int main(int, char**)
{
    const char text[] =
            "HTTP/1.1 200 OK\r\n"
            "Server: nginx/1.2.1\r\n"
            "Content-Type: text/html\r\n"
            "Content-Length: 8\r\n"
            "Connection: keep-alive\r\n"
            "\r\n"
            "<html />";

    Response response;
    HttpResponseParser parser;

    HttpResponseParser::ParseResult res = parser.parse(response, text, text + strlen(text));

    if( res == HttpResponseParser::ParsingCompleted )
    {
        std::cout << response.inspect() << std::endl;
        return EXIT_SUCCESS;
    }
    else
    {
        std::cerr << "Parsing failed" << std::endl;
        return EXIT_FAILURE;
    }
}
#include <iostream>
#include <httpparser/urlparser.h>

using namespace httpparser;

int main(int, char**)
{
    UrlParser parser;

    {
        const char url[] = "git+ssh://example.com/path/file";

        if( parser.parse(url) )
            std::cout << parser.scheme() << "://" << parser.hostname() << std::endl;
        else
            std::cerr << "Can't parse url: " << url << std::endl;
    }

    {
        const char url[] = "https://example.com/path/file";

        if( parser.parse(url) )
            std::cout << parser.scheme() << "://" << parser.hostname() << std::endl;
        else
            std::cerr << "Can't parse url: " << url << std::endl;
    }

    return EXIT_SUCCESS;
}

httpparser's People

Contributors

maywine avatar nekipelov avatar

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.