Refactoring for an improved event loop
This commit is contained in:
@@ -13,17 +13,11 @@ int main(void)
|
||||
HTTP_STR("User-Agent: cHTTP"),
|
||||
};
|
||||
|
||||
// Request handle that will be necessary to
|
||||
// free the request's resources when we are
|
||||
// done with the result.
|
||||
HTTP_RequestHandle handle;
|
||||
|
||||
// Perform the request. This will block the thread
|
||||
// until an error occurs or the request completes.
|
||||
HTTP_Response *res = http_get(
|
||||
HTTP_STR("http://example.com/index.html"),
|
||||
headers, HTTP_COUNT(headers),
|
||||
&handle
|
||||
headers, HTTP_COUNT(headers)
|
||||
);
|
||||
|
||||
// The http_get function returns NULL if the request
|
||||
@@ -51,7 +45,7 @@ int main(void)
|
||||
|
||||
// When we are done reading from the response object
|
||||
// we must free the request's resources.
|
||||
http_request_free(handle);
|
||||
http_request_free(res);
|
||||
|
||||
// All done. Deinitialize the library.
|
||||
http_global_free();
|
||||
|
||||
@@ -12,40 +12,30 @@ int main(int argc, char **argv) {
|
||||
http_global_init();
|
||||
HTTP_Client *client = http_client_init();
|
||||
|
||||
HTTP_RequestHandle reqs[100];
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
int k = i-1;
|
||||
if (http_client_request(client, &reqs[k]) < 0) {
|
||||
HTTP_RequestBuilder builder;
|
||||
if (http_client_get_builder(client, &builder) < 0) {
|
||||
printf("request creation error\n");
|
||||
return -1;
|
||||
}
|
||||
http_request_line(reqs[k], HTTP_METHOD_GET, (HTTP_String) { argv[i], strlen(argv[i]) });
|
||||
http_request_submit(reqs[k]);
|
||||
http_request_builer_line(builder, HTTP_METHOD_GET, (HTTP_String) { argv[i], strlen(argv[i]) });
|
||||
http_request_builder_submit(builder);
|
||||
printf("request submitted\n");
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
if (http_client_wait(client, NULL) < 0) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
|
||||
HTTP_Response *res;
|
||||
if (http_client_wait(client, &res, NULL) < 0) {
|
||||
printf("request wait error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("all requests completed\n");
|
||||
printf("Status: %d\n", res->status);
|
||||
printf("Body: %.*s\n", HTTP_UNPACK(res->body));
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
|
||||
HTTP_Response *result = http_request_result(reqs[i-1]);
|
||||
if (!result) {
|
||||
fprintf(stderr, "No result from HTTP request\n");
|
||||
http_request_free(reqs[i-1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Status: %d\n", result->status);
|
||||
printf("Body: %.*s\n", HTTP_UNPACK(result->body));
|
||||
|
||||
http_request_free(reqs[i-1]);
|
||||
http_request_free(res);
|
||||
}
|
||||
|
||||
http_client_free(client);
|
||||
|
||||
@@ -139,31 +139,26 @@ int main(int argc, char **argv)
|
||||
return -1;
|
||||
}
|
||||
|
||||
HTTP_RequestHandle req;
|
||||
int ret = http_client_request(client, &req);
|
||||
HTTP_RequestBuilder builder;
|
||||
int ret = http_client_get_builder(client, &builder);
|
||||
if (ret < 0) {
|
||||
printf("Couldn't start request\n");
|
||||
http_client_free(client);
|
||||
return -1;
|
||||
}
|
||||
http_request_line(req, HTTP_METHOD_GET, start_url);
|
||||
http_request_header(req, HTTP_STR("User-Agent: Simple crawler"));
|
||||
http_request_submit(req);
|
||||
http_request_builder_line(builder, HTTP_METHOD_GET, start_url);
|
||||
http_request_builder_header(builder, HTTP_STR("User-Agent: Simple crawler"));
|
||||
http_request_builder_submit(builder);
|
||||
|
||||
for (;;) {
|
||||
|
||||
HTTP_RequestHandle req;
|
||||
ret = http_client_wait(client, &req);
|
||||
HTTP_Response *res;
|
||||
ret = http_client_wait(client, &res, NULL);
|
||||
if (ret < 0) {
|
||||
// TODO
|
||||
return -1;
|
||||
}
|
||||
|
||||
HTTP_Response *res = http_request_result(req);
|
||||
if (res == NULL) {
|
||||
http_request_free(req);
|
||||
continue; // Request didn't complete
|
||||
}
|
||||
HTTP_String body = res->body;
|
||||
|
||||
int cursor = 0;
|
||||
@@ -180,14 +175,14 @@ int main(int argc, char **argv)
|
||||
printf("Fetching " GRN "%.*s" RST "\n", HTTP_UNPACK(url));
|
||||
add_to_crawled_list(url);
|
||||
|
||||
HTTP_RequestHandle req;
|
||||
ret = http_client_request(client, &req);
|
||||
HTTP_RequestBuilder builder;
|
||||
ret = http_client_get_builder(client, &builder);
|
||||
if (ret < 0)
|
||||
continue;
|
||||
|
||||
http_request_line(req, HTTP_METHOD_GET, url);
|
||||
http_request_header(req, HTTP_STR("User-Agent: Simple crawler"));
|
||||
http_request_submit(req);
|
||||
http_request_builder_line(builder, HTTP_METHOD_GET, url);
|
||||
http_request_builder_header(builder, HTTP_STR("User-Agent: Simple crawler"));
|
||||
http_request_builder_submit(builder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user