1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
29 /* somewhat unix-specific */
34 #include <curl/curl.h>
36 #ifndef CURLPIPE_MULTIPLEX
37 #error "too old libcurl, can't do HTTP/2 server push!"
41 void dump(const char *text, unsigned char *ptr, size_t size,
47 unsigned int width=0x10;
50 /* without the hex output, we can fit more on screen */
53 fprintf(stderr, "%s, %ld bytes (0x%lx)\n",
54 text, (long)size, (long)size);
56 for(i=0; i<size; i+= width) {
58 fprintf(stderr, "%4.4lx: ", (long)i);
61 /* hex not disabled, show it */
62 for(c = 0; c < width; c++)
64 fprintf(stderr, "%02x ", ptr[i+c]);
69 for(c = 0; (c < width) && (i+c < size); c++) {
70 /* check for 0D0A; if found, skip past and start a new line of output */
71 if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
76 (ptr[i+c]>=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.');
77 /* check again for 0D0A, to avoid an extra \n if it's at width */
78 if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) {
83 fputc('\n', stderr); /* newline */
88 int my_trace(CURL *handle, curl_infotype type,
89 char *data, size_t size,
93 (void)handle; /* prevent compiler warning */
97 fprintf(stderr, "== Info: %s", data);
98 default: /* in case a new one is introduced to shock us */
101 case CURLINFO_HEADER_OUT:
102 text = "=> Send header";
104 case CURLINFO_DATA_OUT:
105 text = "=> Send data";
107 case CURLINFO_SSL_DATA_OUT:
108 text = "=> Send SSL data";
110 case CURLINFO_HEADER_IN:
111 text = "<= Recv header";
113 case CURLINFO_DATA_IN:
114 text = "<= Recv data";
116 case CURLINFO_SSL_DATA_IN:
117 text = "<= Recv SSL data";
121 dump(text, (unsigned char *)data, size, 1);
125 static void setup(CURL *hnd)
127 FILE *out = fopen("dl", "wb");
129 /* write to this file */
130 curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out);
132 /* set the same URL */
133 curl_easy_setopt(hnd, CURLOPT_URL, "https://localhost:8443/index.html");
135 /* send it verbose for max debuggaility */
136 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
137 curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace);
140 curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
142 /* we use a self-signed test server, skip verification during debugging */
143 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
144 curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L);
146 #if (CURLPIPE_MULTIPLEX > 0)
147 /* wait for pipe connection to confirm */
148 curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L);
153 /* called when there's an incoming push */
154 static int server_push_callback(CURL *parent,
157 struct curl_pushheaders *headers,
162 int *transfers = (int *)userp;
165 static unsigned int count = 0;
167 (void)parent; /* we have no use for this */
169 sprintf(filename, "push%u", count++);
171 /* here's a new stream, save it in a new file for each new push */
172 out = fopen(filename, "wb");
174 /* write to this file */
175 curl_easy_setopt(easy, CURLOPT_WRITEDATA, out);
177 fprintf(stderr, "**** push callback approves stream %u, got %d headers!\n",
178 count, (int)num_headers);
180 for(i=0; i<num_headers; i++) {
181 headp = curl_pushheader_bynum(headers, i);
182 fprintf(stderr, "**** header %u: %s\n", (int)i, headp);
185 headp = curl_pushheader_byname(headers, ":path");
187 fprintf(stderr, "**** The PATH is %s\n", headp /* skip :path + colon */ );
190 (*transfers)++; /* one more */
196 * Download a file over HTTP/2, take care of server push.
202 int still_running; /* keep number of running handles */
203 int transfers=1; /* we start with one */
206 /* init a multi stack */
207 multi_handle = curl_multi_init();
209 easy = curl_easy_init();
214 /* add the easy transfer */
215 curl_multi_add_handle(multi_handle, easy);
217 curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
218 curl_multi_setopt(multi_handle, CURLMOPT_PUSHFUNCTION, server_push_callback);
219 curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers);
221 /* we start some action by calling perform right away */
222 curl_multi_perform(multi_handle, &still_running);
225 struct timeval timeout;
226 int rc; /* select() return code */
227 CURLMcode mc; /* curl_multi_fdset() return code */
234 long curl_timeo = -1;
240 /* set a suitable timeout to play around with */
244 curl_multi_timeout(multi_handle, &curl_timeo);
245 if(curl_timeo >= 0) {
246 timeout.tv_sec = curl_timeo / 1000;
247 if(timeout.tv_sec > 1)
250 timeout.tv_usec = (curl_timeo % 1000) * 1000;
253 /* get file descriptors from the transfers */
254 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
257 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
261 /* On success the value of maxfd is guaranteed to be >= -1. We call
262 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
263 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
264 to sleep 100ms, which is the minimum suggested value in the
265 curl_multi_fdset() doc. */
272 /* Portable sleep for platforms other than Windows. */
273 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
274 rc = select(0, NULL, NULL, NULL, &wait);
278 /* Note that on some platforms 'timeout' may be modified by select().
279 If you need access to the original value save a copy beforehand. */
280 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
289 /* timeout or readable/writable sockets */
290 curl_multi_perform(multi_handle, &still_running);
295 * A little caution when doing server push is that libcurl itself has
296 * created and added one or more easy handles but we need to clean them up
302 m = curl_multi_info_read(multi_handle, &msgq);
303 if(m && (m->msg == CURLMSG_DONE)) {
304 CURL *e = m->easy_handle;
306 curl_multi_remove_handle(multi_handle, e);
307 curl_easy_cleanup(e);
311 } while(transfers); /* as long as we have transfers going */
313 curl_multi_cleanup(multi_handle);