Giter VIP home page Giter VIP logo

httpparser's Introduction

HTTP Parser

tests codecov Codacy Badge

HTTP Parser provides the functionality to parse both HTTP requests and responses. Written in C and was created for working in embedded applications.

Features

  • All dependencies in one package
  • Request/Response validation
  • Decodes chunked encoding
  • Easy to manipulate with headers and query parameters(see example)

The parser extracts the following information from HTTP messages:

  • HTTP version
  • Content-Length
  • Request method
  • Response status code
  • Request URL
  • Transfer-Encoding
  • Message body
  • Headers as key and value pairs
  • URL query parameters as key and value pairs

Add as CPM project dependency

How to add CPM to the project, check the link

CPMAddPackage(
        NAME HTTPParser
        GITHUB_REPOSITORY ximtech/HTTPParser
        GIT_TAG origin/main)

target_link_libraries(${PROJECT_NAME} HTTPParser)
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})
# For Clion STM32 plugin generated Cmake use 
target_link_libraries(${PROJECT_NAME}.elf HTTPParser)

Usage

NOTE: Parser use split function. Character buffer will be spoiled

const char *testRequest =
        "GET /cgi-bin/process.cgi?param1=val1&param2=val2 HTTP/1.1\n"
        "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\n"
        "Host: www.example.com\n"
        "Content-Type: text/xml; charset=utf-8\n"
        "Content-Length: 12345\n"
        "Accept-Language: en-us\n"
        "Accept-Encoding: gzip, deflate\n"
        "Connection: Keep-Alive\n\n";

char httpDataBuffer[1000];
strcpy(httpDataBuffer, testRequest);

HTTPParser *parser = getHttpParserInstance();
parseHttpBuffer(httpDataBuffer, parser, HTTP_REQUEST);  // Parse request
if (parser->parserStatus == HTTP_PARSE_OK) {    // Parser checks for errors
    printf("Http Version: %s\n", parser->httpVersion);
    printf("Content-Length: %ul\n", parser->contentLength);
    printf("Method: %s\n", getHttpMethodName(parser->method));
    printf("URI path: %s\n\n", parser->uriPath);
}

// Parse headers
parseHttpHeaders(parser, httpDataBuffer);
HashMap headers = parser->headers;
printf("Header count: %d\n", getHashMapSize(headers));
HashMapIterator headerIterator = getHashMapIterator(headers);
while (hashMapHasNext(&headerIterator)) {
    printf("[%s]: [%s]\n", headerIterator.key, headerIterator.value);
}

printf("\n");

// Parse URL parameters
parseHttpQueryParameters(parser, httpDataBuffer);
HashMap params = parser->queryParameters;
printf("Params count: %d\n", getHashMapSize(params));
HashMapIterator paramIterator = getHashMapIterator(params);
while (hashMapHasNext(&paramIterator)) {
    printf("[%s]: [%s]\n", paramIterator.key, paramIterator.value);
}

deleteHttpParser(parser);

httpparser's People

Contributors

ximtech avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

rjjrbatarao

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.