MCPcopy Create free account
hub / github.com/BernardoGiordano/Checkpoint / section_network

Function section_network

scripts/examples/example.c:707–768  ·  view source on GitHub ↗

web_get is the one-liner: it returns the HTTP status, or negative on failure * (-1 no HTTP available, -2 the response did not fit in memory, -(CURLcode+100) * for a transfer error). -2 is the one worth retrying with a smaller request. * * web_request is the general form — method, headers, request body, and the raw * response headers back. web_upload_file is web_request with the body streamed

Source from the content-addressed store, hash-verified

705 * Response buffers are yours to free(). net_ip() answers "0.0.0.0" with no
706 * network, which is the cheapest way to check for connectivity. */
707void section_network(void)
708{
709 char line[LINEN];
710 char* ip;
711 char* body;
712 char* headers;
713 char* ctype;
714 char* encoded;
715 int size;
716 int status;
717
718 gui_status("Network demo");
719
720 ip = net_ip();
721 printf("net_ip: %s\n", ip);
722 if (strcmp(ip, "0.0.0.0") == 0) {
723 free(ip);
724 gui_message("No network connection — skipping the HTTP calls.");
725 return;
726 }
727 free(ip);
728
729 /* url_encode: percent-encoding for query strings and form bodies. */
730 encoded = url_encode("a b&c=d/e");
731 printf("url_encode(\"a b&c=d/e\") = %s\n", encoded);
732 free(encoded);
733
734 if (!gui_confirm("Fetch http://example.com to demonstrate web_get and web_request?")) {
735 return;
736 }
737
738 progress_note("web_get");
739 status = web_get(&body, &size, "http://example.com");
740 printf("web_get: status=%d size=%d\n", status, size);
741 if (status == 200) {
742 free(body);
743 }
744
745 /* The same fetch through web_request, with a header of our own, and then
746 * one header read back out of the raw response block. Headers are
747 * "\n"-separated "Key: Value" lines; "" means none. The body is "" / 0 for
748 * a request that has none. Pass a valid char** for the response headers
749 * even if you ignore them. */
750 progress_note("web_request");
751 status = web_request("GET", "http://example.com", "User-Agent: Checkpoint-example/1.0", "", 0, &body, &size, &headers);
752 printf("web_request: status=%d size=%d\n", status, size);
753 if (status >= 0) {
754 ctype = http_header_value(headers, "Content-Type");
755 sprintf(line, "HTTP %d, %d bytes, Content-Type: %s", status, size, ctype);
756 printf("%s\n", line);
757 gui_message(line);
758 free(ctype);
759 free(headers);
760 free(body);
761 }
762 else {
763 sprintf(line, "web_request failed: %d", status);
764 logline(line);

Callers 1

mainFunction · 0.85

Calls 1

loglineFunction · 0.85

Tested by

no test coverage detected