123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <curl/curl.h>
- #include <curl/mprintf.h>
- #ifndef CURLPIPE_MULTIPLEX
- #error "too old libcurl, cannot do HTTP/2 server push!"
- #endif
- static
- void dump(const char *text, unsigned char *ptr, size_t size,
- char nohex)
- {
- size_t i;
- size_t c;
- unsigned int width = 0x10;
- if(nohex)
-
- width = 0x40;
- fprintf(stderr, "%s, %lu bytes (0x%lx)\n",
- text, (unsigned long)size, (unsigned long)size);
- for(i = 0; i < size; i += width) {
- fprintf(stderr, "%4.4lx: ", (unsigned long)i);
- if(!nohex) {
-
- for(c = 0; c < width; c++)
- if(i + c < size)
- fprintf(stderr, "%02x ", ptr[i + c]);
- else
- fputs(" ", stderr);
- }
- for(c = 0; (c < width) && (i + c < size); c++) {
-
- if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
- ptr[i + c + 1] == 0x0A) {
- i += (c + 2 - width);
- break;
- }
- fprintf(stderr, "%c",
- (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
-
- if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
- ptr[i + c + 2] == 0x0A) {
- i += (c + 3 - width);
- break;
- }
- }
- fputc('\n', stderr);
- }
- }
- static
- int my_trace(CURL *handle, curl_infotype type,
- char *data, size_t size,
- void *userp)
- {
- const char *text;
- (void)handle;
- (void)userp;
- switch(type) {
- case CURLINFO_TEXT:
- fprintf(stderr, "== Info: %s", data);
- return 0;
- case CURLINFO_HEADER_OUT:
- text = "=> Send header";
- break;
- case CURLINFO_DATA_OUT:
- text = "=> Send data";
- break;
- case CURLINFO_SSL_DATA_OUT:
- text = "=> Send SSL data";
- break;
- case CURLINFO_HEADER_IN:
- text = "<= Recv header";
- break;
- case CURLINFO_DATA_IN:
- text = "<= Recv data";
- break;
- case CURLINFO_SSL_DATA_IN:
- text = "<= Recv SSL data";
- break;
- default:
- return 0;
- }
- dump(text, (unsigned char *)data, size, 1);
- return 0;
- }
- #define OUTPUTFILE "dl"
- static int setup(CURL *hnd, const char *url)
- {
- FILE *out = fopen(OUTPUTFILE, "wb");
- if(!out)
-
- return 1;
-
- curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
-
- curl_easy_setopt(hnd, CURLOPT_URL, url);
-
- curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
-
- curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
-
- curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
- curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
- #if (CURLPIPE_MULTIPLEX > 0)
-
- curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
- #endif
- return 0;
- }
- static int server_push_callback(CURL *parent,
- CURL *easy,
- size_t num_headers,
- struct curl_pushheaders *headers,
- void *userp)
- {
- char *headp;
- size_t i;
- int *transfers = (int *)userp;
- char filename[128];
- FILE *out;
- static unsigned int count = 0;
- (void)parent;
- curl_msnprintf(filename, 128, "push%u", count++);
-
- out = fopen(filename, "wb");
- if(!out) {
-
- fprintf(stderr, "Failed to create output file for push\n");
- return CURL_PUSH_DENY;
- }
-
- curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
- fprintf(stderr, "**** push callback approves stream %u, got %lu headers!\n",
- count, (unsigned long)num_headers);
- for(i = 0; i < num_headers; i++) {
- headp = curl_pushheader_bynum(headers, i);
- fprintf(stderr, "**** header %lu: %s\n", (unsigned long)i, headp);
- }
- headp = curl_pushheader_byname(headers, ":path");
- if(headp) {
- fprintf(stderr, "**** The PATH is %s\n", headp );
- }
- (*transfers)++;
- return CURL_PUSH_OK;
- }
- int main(int argc, char *argv[])
- {
- CURL *easy;
- CURLM *multi_handle;
- int transfers = 1;
- struct CURLMsg *m;
- const char *url = "https://localhost:8443/index.html";
- if(argc == 2)
- url = argv[1];
-
- multi_handle = curl_multi_init();
- easy = curl_easy_init();
-
- if(setup(easy, url)) {
- fprintf(stderr, "failed\n");
- return 1;
- }
-
- curl_multi_add_handle(multi_handle, easy);
- curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
- curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
- curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
- do {
- int still_running;
- CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
- if(still_running)
-
- mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
- if(mc)
- break;
-
- do {
- int msgq = 0;
- m = curl_multi_info_read(multi_handle, &msgq);
- if(m && (m->msg == CURLMSG_DONE)) {
- CURL *e = m->easy_handle;
- transfers--;
- curl_multi_remove_handle(multi_handle, e);
- curl_easy_cleanup(e);
- }
- } while(m);
- } while(transfers);
- curl_multi_cleanup(multi_handle);
- return 0;
- }
|