Giter Site home page Giter Site logo

libhttp's Introduction

libhttp

Light-weight C library for HTTP/1.1 requests. Meant for embedded environments or whereever libcurl would be just too much.

Fitness

If there's any chance you will ever need to make HTTPS requests or do anything else than just plain simple HTTP/1.1 requests, use libcurl.

This library aims to remain as simple and portable as possible to offer a nice and small alternative.

Sample

Simple GET request

For a simple GET request you just need to call http_request() and http_response():

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "http.h"

int main(int argc, char **argv) {
	int sd;
	struct http_message msg;

	if (!--argc || (sd = http_request(*++argv)) < 1) {
		perror("http_request");
		return -1;
	}

	memset(&msg, 0, sizeof(msg));

	while (http_response(sd, &msg) > 0) {
		if (msg.content) {
			write(1, msg.content, msg.length);
		}
	}

	close(sd);

	if (msg.header.code != 200) {
		fprintf(
			stderr,
			"error: returned HTTP code %d\n",
			msg.header.code);
	}

	return 0;
}

Custom request

If you want to add/tweak some header values or do a POST request, use the low level functions http_parse_url(), http_connect() and http_send() to compose your request like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "http.h"

int post(int sd, struct http_url *url) {
	char buf[1024];

	snprintf(
		buf,
		sizeof(buf),
		"\
POST /%s HTTP/1.1\r\n\
User-Agent: Mozilla/4.0 (Linux)\r\n\
Host: %s\r\n\
Accept: */*\r\n\
Content-Length: 13\r\n\
Connection: close\r\n\
\r\n\
q=Test&btn=Go\r\n\
\r\n",
		url->query,
		url->host);

	if (http_send(sd, buf)) {
		perror("http_send");
		return -1;
	}

	return 0;
}

int main(int argc, char **argv) {
	struct http_url *url;
	struct http_message msg;
	int sd;

	if (!(url = http_parse_url(*++argv)) ||
			!(sd = http_connect(url))) {
		free(url);
		perror("http_connect");
		return -1;
	}

	memset(&msg, 0, sizeof(msg));

	if (!post(sd, url)) {
		while (http_response(sd, &msg) > 0) {
			if (msg.content) {
				write(1, msg.content, msg.length);
			}
		}
	}

	free(url);
	close(sd);

	if (msg.header.code != 200) {
		fprintf(
			stderr,
			"error: returned HTTP code %d\n",
			msg.header.code);
	}

	return 0;
}

Testing

There's a bash script to test any URL list against libhttp:

$ cd test && bash test.sh [FILE-WITH-URLS]

If you find bugs, please report them.

libhttp's People

Contributors

markusfisch avatar

Watchers

James Cloos 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.