Fix HTTP request creation and simple client example
This commit is contained in:
@@ -2305,7 +2305,12 @@ static void socket_update(Socket *s)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AddressAndPort addr = s->addrs[s->next_addr];
|
|
||||||
|
AddressAndPort addr;
|
||||||
|
if (s->num_addr == 1)
|
||||||
|
addr = s->addr;
|
||||||
|
else
|
||||||
|
addr = s->addrs[s->next_addr];
|
||||||
|
|
||||||
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
|
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
|
||||||
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
|
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
|
||||||
@@ -2972,7 +2977,6 @@ int socket_connect(SocketManager *sm, int num_targets,
|
|||||||
|
|
||||||
s->state = SOCKET_STATE_PENDING;
|
s->state = SOCKET_STATE_PENDING;
|
||||||
s->sock = NATIVE_SOCKET_INVALID;
|
s->sock = NATIVE_SOCKET_INVALID;
|
||||||
s->events = 0;
|
|
||||||
s->user = user;
|
s->user = user;
|
||||||
#ifdef HTTPS_ENABLED
|
#ifdef HTTPS_ENABLED
|
||||||
s->server_secure_context = NULL;
|
s->server_secure_context = NULL;
|
||||||
@@ -2982,6 +2986,8 @@ int socket_connect(SocketManager *sm, int num_targets,
|
|||||||
s->client_secure_context = &sm->client_secure_context;
|
s->client_secure_context = &sm->client_secure_context;
|
||||||
#endif
|
#endif
|
||||||
sm->num_used++;
|
sm->num_used++;
|
||||||
|
|
||||||
|
socket_update(s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3902,16 +3908,22 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
|
|||||||
// Write method
|
// Write method
|
||||||
HTTP_String method_str = get_method_string(method);
|
HTTP_String method_str = get_method_string(method);
|
||||||
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
|
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
|
||||||
|
byte_queue_write(&conn->output, " ", 1);
|
||||||
|
|
||||||
|
// Write path
|
||||||
|
if (conn->url.path.len == 0)
|
||||||
|
byte_queue_write(&conn->output, "/", 1);
|
||||||
|
else
|
||||||
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
|
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
|
||||||
|
|
||||||
|
// Write query string
|
||||||
HTTP_String query = conn->url.query;
|
HTTP_String query = conn->url.query;
|
||||||
if (query.len > 0) {
|
if (query.len > 0) {
|
||||||
byte_queue_write(&conn->output, "?", 1);
|
byte_queue_write(&conn->output, "?", 1);
|
||||||
byte_queue_write(&conn->output, query.ptr, query.len);
|
byte_queue_write(&conn->output, query.ptr, query.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTP_String version = HTTP_STR("HTTP/1.1");
|
HTTP_String version = HTTP_STR(" HTTP/1.1");
|
||||||
byte_queue_write(&conn->output, version.ptr, version.len);
|
byte_queue_write(&conn->output, version.ptr, version.len);
|
||||||
|
|
||||||
byte_queue_write(&conn->output, "\r\n", 2);
|
byte_queue_write(&conn->output, "\r\n", 2);
|
||||||
|
|||||||
@@ -48,12 +48,17 @@ int main(void)
|
|||||||
if (http_client_process_events(&client, ®) < 0)
|
if (http_client_process_events(&client, ®) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
HTTP_Response *response;
|
HTTP_Response *response;
|
||||||
void *user;
|
void *user;
|
||||||
while (http_client_next_response(&client, &response, &user)) {
|
while (http_client_next_response(&client, &response, &user)) {
|
||||||
printf("Received response\n");
|
printf("Received response\n");
|
||||||
http_free_response(response);
|
http_free_response(response);
|
||||||
|
done = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (done) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
http_client_free(&client);
|
http_client_free(&client);
|
||||||
|
|||||||
+7
-1
@@ -249,16 +249,22 @@ void http_request_builder_url(HTTP_RequestBuilder builder,
|
|||||||
// Write method
|
// Write method
|
||||||
HTTP_String method_str = get_method_string(method);
|
HTTP_String method_str = get_method_string(method);
|
||||||
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
|
byte_queue_write(&conn->output, method_str.ptr, method_str.len);
|
||||||
|
byte_queue_write(&conn->output, " ", 1);
|
||||||
|
|
||||||
|
// Write path
|
||||||
|
if (conn->url.path.len == 0)
|
||||||
|
byte_queue_write(&conn->output, "/", 1);
|
||||||
|
else
|
||||||
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
|
byte_queue_write(&conn->output, conn->url.path.ptr, conn->url.path.len);
|
||||||
|
|
||||||
|
// Write query string
|
||||||
HTTP_String query = conn->url.query;
|
HTTP_String query = conn->url.query;
|
||||||
if (query.len > 0) {
|
if (query.len > 0) {
|
||||||
byte_queue_write(&conn->output, "?", 1);
|
byte_queue_write(&conn->output, "?", 1);
|
||||||
byte_queue_write(&conn->output, query.ptr, query.len);
|
byte_queue_write(&conn->output, query.ptr, query.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTP_String version = HTTP_STR("HTTP/1.1");
|
HTTP_String version = HTTP_STR(" HTTP/1.1");
|
||||||
byte_queue_write(&conn->output, version.ptr, version.len);
|
byte_queue_write(&conn->output, version.ptr, version.len);
|
||||||
|
|
||||||
byte_queue_write(&conn->output, "\r\n", 2);
|
byte_queue_write(&conn->output, "\r\n", 2);
|
||||||
|
|||||||
+8
-1
@@ -335,7 +335,12 @@ static void socket_update(Socket *s)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AddressAndPort addr = s->addrs[s->next_addr];
|
|
||||||
|
AddressAndPort addr;
|
||||||
|
if (s->num_addr == 1)
|
||||||
|
addr = s->addr;
|
||||||
|
else
|
||||||
|
addr = s->addrs[s->next_addr];
|
||||||
|
|
||||||
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
|
int family = (addr.is_ipv4 ? AF_INET : AF_INET6);
|
||||||
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
|
NATIVE_SOCKET sock = socket(family, SOCK_STREAM, 0);
|
||||||
@@ -1011,6 +1016,8 @@ int socket_connect(SocketManager *sm, int num_targets,
|
|||||||
s->client_secure_context = &sm->client_secure_context;
|
s->client_secure_context = &sm->client_secure_context;
|
||||||
#endif
|
#endif
|
||||||
sm->num_used++;
|
sm->num_used++;
|
||||||
|
|
||||||
|
socket_update(s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user