Add a client example program and move examples to the examples/ folder
This commit is contained in:
+4
-2
@@ -1,2 +1,4 @@
|
||||
example
|
||||
example.exe
|
||||
simple_client
|
||||
simple_client.exe
|
||||
simple_server
|
||||
simple_server.exe
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
.PHONY: all clean example
|
||||
|
||||
all: chttp.c chttp.h example
|
||||
all: chttp.c chttp.h simple_client simple_server
|
||||
|
||||
chttp.c chttp.h: $(wildcard src/*.c src/*.h) misc/amalg.py Makefile
|
||||
python misc/amalg.py
|
||||
|
||||
example: main.c chttp.c chttp.h
|
||||
gcc main.c chttp.c -o example -ggdb
|
||||
%: examples/%.c chttp.c chttp.h
|
||||
gcc $< chttp.c -o $@ -ggdb
|
||||
|
||||
clean:
|
||||
rm chttp.c chttp.h
|
||||
|
||||
@@ -2372,7 +2372,8 @@ static int socket_manager_register_events_nolock(
|
||||
continue;
|
||||
j++;
|
||||
|
||||
// TODO: comment
|
||||
// If at least one socket can be processed, return an
|
||||
// empty list.
|
||||
if (s->state == SOCKET_STATE_DIED || s->state == SOCKET_STATE_ESTABLISHED_READY) {
|
||||
reg->num_polled = 0;
|
||||
return 0;
|
||||
@@ -3814,12 +3815,12 @@ bool http_client_next_response(HTTP_Client *client,
|
||||
return true;
|
||||
}
|
||||
|
||||
void http_free_response(HTTP_Response *res)
|
||||
void http_free_response(HTTP_Response *response)
|
||||
{
|
||||
if (res == NULL || res->context == NULL)
|
||||
if (response == NULL || response->context == NULL)
|
||||
return;
|
||||
|
||||
HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context;
|
||||
HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context;
|
||||
|
||||
// Free the connection resources
|
||||
http_client_conn_free(conn);
|
||||
@@ -3827,7 +3828,7 @@ void http_free_response(HTTP_Response *res)
|
||||
conn->client->num_conns--;
|
||||
|
||||
// Mark response as freed
|
||||
res->context = NULL;
|
||||
response->context = NULL;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -910,7 +910,7 @@ bool http_client_next_response(HTTP_Client *client,
|
||||
|
||||
// Free a response object. You can't access its fields
|
||||
// again after this.
|
||||
void http_free_response(HTTP_Response *res);
|
||||
void http_free_response(HTTP_Response *response);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// src/server.h
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "../chttp.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define POLL WSAPoll
|
||||
#else
|
||||
#define POLL poll
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
HTTP_Client client;
|
||||
if (http_client_init(&client) < 0)
|
||||
return -1;
|
||||
|
||||
HTTP_RequestBuilder builder;
|
||||
if (http_client_get_builder(&client, NULL, &builder) < 0)
|
||||
return -1;
|
||||
|
||||
http_request_builder_url(builder,
|
||||
HTTP_METHOD_GET,
|
||||
HTTP_STR("http://coz.is")
|
||||
);
|
||||
|
||||
http_request_builder_header(builder,
|
||||
HTTP_STR("Greeting: Hello from the cHTTP example!"));
|
||||
|
||||
if (http_request_builder_send(builder) < 0)
|
||||
return -1;
|
||||
|
||||
for (;;) {
|
||||
|
||||
void *ptrs[HTTP_CLIENT_CAPACITY+1];
|
||||
struct pollfd polled[HTTP_CLIENT_CAPACITY+1];
|
||||
|
||||
EventRegister reg = {
|
||||
.ptrs=ptrs,
|
||||
.polled=polled,
|
||||
.num_polled=0,
|
||||
.max_polled=HTTP_CLIENT_CAPACITY+1,
|
||||
};
|
||||
|
||||
if (http_client_register_events(&client, ®) < 0)
|
||||
return -1;
|
||||
|
||||
if (reg.num_polled > 0)
|
||||
POLL(reg.polled, reg.num_polled, -1);
|
||||
|
||||
if (http_client_process_events(&client, ®) < 0)
|
||||
return -1;
|
||||
|
||||
HTTP_Response *response;
|
||||
while (http_client_next_response(&client, &response)) {
|
||||
// TODO
|
||||
http_free_response(response);
|
||||
}
|
||||
}
|
||||
|
||||
http_client_free(&client);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "chttp.h"
|
||||
#include "../chttp.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define POLL WSAPoll
|
||||
+4
-4
@@ -422,12 +422,12 @@ bool http_client_next_response(HTTP_Client *client,
|
||||
return true;
|
||||
}
|
||||
|
||||
void http_free_response(HTTP_Response *res)
|
||||
void http_free_response(HTTP_Response *response)
|
||||
{
|
||||
if (res == NULL || res->context == NULL)
|
||||
if (response == NULL || response->context == NULL)
|
||||
return;
|
||||
|
||||
HTTP_ClientConn *conn = (HTTP_ClientConn*) res->context;
|
||||
HTTP_ClientConn *conn = (HTTP_ClientConn*) response->context;
|
||||
|
||||
// Free the connection resources
|
||||
http_client_conn_free(conn);
|
||||
@@ -435,5 +435,5 @@ void http_free_response(HTTP_Response *res)
|
||||
conn->client->num_conns--;
|
||||
|
||||
// Mark response as freed
|
||||
res->context = NULL;
|
||||
response->context = NULL;
|
||||
}
|
||||
|
||||
+1
-1
@@ -151,4 +151,4 @@ bool http_client_next_response(HTTP_Client *client,
|
||||
|
||||
// Free a response object. You can't access its fields
|
||||
// again after this.
|
||||
void http_free_response(HTTP_Response *res);
|
||||
void http_free_response(HTTP_Response *response);
|
||||
|
||||
Reference in New Issue
Block a user