Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: markusfisch/libhttp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: lazy-eggplant/libhttp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 7 files changed
  • 2 contributors

Commits on Dec 14, 2024

  1. Introducing meson

    karurochari committed Dec 14, 2024
    Copy the full SHA
    9e32b81 View commit details

Commits on Dec 28, 2024

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    4e69042 View commit details
Showing with 122 additions and 100 deletions.
  1. +0 −15 Makefile
  2. +91 −85 README.md
  3. 0 { → include}/http.h
  4. +30 −0 meson.build
  5. +1 −0 meson.options
  6. 0 { → src}/http.c
  7. 0 test/meson.build
15 changes: 0 additions & 15 deletions Makefile

This file was deleted.

176 changes: 91 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
@@ -22,121 +22,127 @@ Sample
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>
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "http.h"
#include "http.h"

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

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

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

while (http_response(sd, &msg) > 0) {
if (msg.content) {
write(1, msg.content, msg.length);
}
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);
}
close(sd);

return 0;
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;
```c
#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;
}
int main(int argc, char **argv) {
struct http_url *url;
struct http_message msg;
int sd;
return 0;
}
if (!(url = http_parse_url(*++argv)) ||
!(sd = http_connect(url))) {
free(url);
perror("http_connect");
return -1;
}
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));
memset(&msg, 0, sizeof(msg));
if (!post(sd, url)) {
while (http_response(sd, &msg) > 0) {
if (msg.content) {
write(1, msg.content, msg.length);
}
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);
}
free(url);
close(sd);
return 0;
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]
```bash
$ cd test && bash test.sh [FILE-WITH-URLS]
```

If you find bugs, please report them.

File renamed without changes.
30 changes: 30 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
project('http', 'c', version: '1.0.0')

src = files('./src/http.c')

include_dir = include_directories('./include')

http = library(
'http',
src,
include_directories: [include_dir],
install: not meson.is_subproject(),
)
http_dep = declare_dependency(
include_directories: include_dir,
link_with: http,
)

pkg_mod = import('pkgconfig')

pkg_mod.generate(
http,
name: 'http',
description: 'Simple http client',
libraries: http,
version: meson.project_version(),
)

if get_variable('tests')
subdir('./test')
endif
1 change: 1 addition & 0 deletions meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('tests', type: 'boolean', value: true)
File renamed without changes.
Empty file added test/meson.build
Empty file.