Giter Site home page Giter Site logo

fmorurl's Introduction

fmorurl

C89 Url parser that does no malloc neither it modify the supplied string. No validation is performed.

This library does NOT support UTF strings.

Parsing url

call url_parse function to fill a an url struct which is defined as follow :

struct url_str
{
    const char* str;
    int         size;
};

struct url
{
    struct url_str scheme;
    struct url_str user;
    struct url_str password;
    struct url_str hostname;
    struct url_str port;
    struct url_str path;
    struct url_str query;
    struct url_str fragment;
};
#include <string.h>
#include <url_parser.h>

int main()
{
    int r;
    struct url u;

    const char* url =  "http://john:[email protected]:8888/path/to/file?param=value#fragment";

    r = url_parse( &u, url, strlen(url) );
    if( r != url_parse_ok )
        return -1;

    url_dump( &u, url );

    return 0;
}

Parsing url query

Use url_parse_query, it fill an array of url_query_param structs and returns the number of key value pairs ( even if one of the two is null ) or < 0 in case of error.

struct url_query_param
{
    struct url_str name;
    struct url_str value;
};
#include <stdio.h>
#include <string.h>

#include <urlparser.h>

#define QUERY_PARAMS_MAX    3

int main()
{
    int r;
    struct url u;
    struct url_query_param params[QUERY_PARAMS_MAX];

    const char* url =  "http://john:[email protected]:8888/path/to/file?param0=value0&param1=value1#fragment";

    r = url_parse( &u, url, strlen(url) );
    if( r != url_parse_ok )
        return -1;

    url_dump( &u, url );


    // query
    printf( "url query params : '%.*s'\n", u.query.size, u.query.str );
    r = url_parse_query( params, QUERY_PARAMS_MAX, '&', u.query.str, u.query.size );
    if( r < 0 )
        return -1;

    url_dump_query_params( params, r );

    // query parse : supplied array too small
    printf( "url query params : '%.*s'\n", u.query.size, u.query.str );
    r = url_parse_query( params, 1 , '&', u.query.str, u.query.size );
    if( r == url_parse_query_error_params_count )
        printf( "params is too small\n");
    else
        return -1;

    return 0;
}

Include in your project

Just drop src/url_parser.h and src/url_parser.c in your project sources directory, or wrap it with meson.

Meson options

Name Description Type Default
fmorurl_build_tests build test executable boolean true

fmorurl's People

Watchers

 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.