Add http_get, http_post, and the first client example
This commit is contained in:
@@ -47,6 +47,9 @@ HTTP_String http_trim(HTTP_String s);
|
||||
// Returns the number of items of a static array.
|
||||
#define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0]))
|
||||
|
||||
// TODO: comment
|
||||
#define HTTP_UNPACK(X) (X).len, (X).ptr
|
||||
|
||||
// Macro used to make invariants of the code more explicit.
|
||||
//
|
||||
// Say you have some function that operates on two integers
|
||||
|
||||
@@ -370,4 +370,83 @@ void http_request_free(HTTP_RequestHandle handle)
|
||||
socket_free(&conn->socket);
|
||||
conn->state = CLIENT_CONNECTION_FREE;
|
||||
client->num_conns--;
|
||||
}
|
||||
|
||||
static HTTP_Client *default_client___; // TODO: deinitialize the default client when http_global_free is called
|
||||
|
||||
static HTTP_Client *get_default_client(void)
|
||||
{
|
||||
if (default_client___ == NULL)
|
||||
default_client___ = http_client_init();
|
||||
return default_client___;
|
||||
}
|
||||
|
||||
HTTP_Response *http_get(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_RequestHandle *phandle)
|
||||
{
|
||||
HTTP_Client *client = get_default_client();
|
||||
if (client == NULL)
|
||||
return NULL;
|
||||
|
||||
HTTP_RequestHandle handle;
|
||||
int ret = http_client_request(client, &handle);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
http_request_line(handle, HTTP_METHOD_GET, url);
|
||||
|
||||
for (int i = 0; i < num_headers; i++)
|
||||
http_request_header(handle, headers[i]);
|
||||
|
||||
http_request_submit(handle);
|
||||
|
||||
ret = http_client_wait(client, NULL); // TODO: it's assumed there is only one request pending
|
||||
if (ret < 0) {
|
||||
http_request_free(handle); // TODO: currently free only works on completed request handles
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HTTP_Response *res = http_request_result(handle);
|
||||
if (res == NULL) {
|
||||
http_request_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*phandle = handle;
|
||||
return res;
|
||||
}
|
||||
|
||||
HTTP_Response *http_post(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_String body, HTTP_RequestHandle *phandle)
|
||||
{
|
||||
HTTP_Client *client = get_default_client();
|
||||
if (client == NULL)
|
||||
return NULL;
|
||||
|
||||
HTTP_RequestHandle handle;
|
||||
int ret = http_client_request(client, &handle);
|
||||
if (ret < 0)
|
||||
return NULL;
|
||||
|
||||
http_request_line(handle, HTTP_METHOD_GET, url);
|
||||
|
||||
for (int i = 0; i < num_headers; i++)
|
||||
http_request_header(handle, headers[i]);
|
||||
|
||||
http_request_body(handle, body);
|
||||
|
||||
http_request_submit(handle);
|
||||
|
||||
ret = http_client_wait(client, NULL); // TODO: it's assumed there is only one request pending
|
||||
if (ret < 0) {
|
||||
http_request_free(handle); // TODO: currently free only works on completed request handles
|
||||
return NULL;
|
||||
}
|
||||
|
||||
HTTP_Response *res = http_request_result(handle);
|
||||
if (res == NULL) {
|
||||
http_request_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*phandle = handle;
|
||||
return res;
|
||||
}
|
||||
@@ -94,4 +94,14 @@ HTTP_Response *http_request_result(HTTP_RequestHandle handle);
|
||||
// TODO: allow aborting pending requests
|
||||
void http_request_free(HTTP_RequestHandle handle);
|
||||
|
||||
// TODO: comment
|
||||
HTTP_Response *http_get(HTTP_String url,
|
||||
HTTP_String *headers, int num_headers,
|
||||
HTTP_RequestHandle *phandle);
|
||||
|
||||
// TODO: comment
|
||||
HTTP_Response *http_post(HTTP_String url,
|
||||
HTTP_String *headers, int num_headers,
|
||||
HTTP_String body, HTTP_RequestHandle *phandle);
|
||||
|
||||
#endif // CLIENT_INCLUDED
|
||||
Reference in New Issue
Block a user