Debian Codegraph @

src/httpget.c

raw
1 /*
2 httpget.c: http communication
3
4 copyright ?-2020 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written Oliver Fromme
7 old timestamp: Wed Apr 9 20:57:47 MET DST 1997
8
9 Thomas' notes:
10 ;
11 I used to do
12 GET http://server/path HTTP/1.0
13
14 But RFC 1945 says: The absoluteURI form is only allowed when the request is being made to a proxy.
15
16 so I should not do that. Since name based virtual hosts need the hostname in the request, I still need to provide that info.
17 Enter HTTP/1.1... there is a Host eader field to use (that mpg123 supposedly has used since some time anyway - but did it really work with my vhost test server)?
18 Now
19 GET /path/bla HTTP/1.1\r\nHost: host[:port]
20 Should work, but as a funny sidenote:
21
22 RFC2616: To allow for transition to absoluteURIs in all requests in future versions of HTTP, all HTTP/1.1 servers MUST accept the absoluteURI form in requests, even though HTTP/1.1 clients will only generate them in requests to proxies.
23
24 I was already full-on HTTP/1.1 as I recognized that mpg123 then would have to accept the chunked transfer encoding.
25 That is not desireable for its purpose... maybe when interleaving of shoutcasts with metadata chunks is supported, we can upgrade to 1.1.
26 Funny aspect there is that shoutcast servers do not do HTTP/1.1 chunked transfer but implement some different chunking themselves...
27 */
28
29 #include "mpg123app.h"
30 #include "httpget.h"
31 #include "version.h"
32
33 #ifdef NETWORK
34 #include "resolver.h"
35
36 #include <errno.h>
37 #include "common/true.h"
38 #endif
39
40 #include <ctype.h>
41
42 #include "common/debug.h"
43
44 void httpdata_init(struct httpdata *e)
45 {
46 mpg123_init_string(&e->content_type);
47 mpg123_init_string(&e->icy_url);
48 mpg123_init_string(&e->icy_name);
49 e->icy_interval = 0;
50 e->proxystate = PROXY_UNKNOWN;
51 mpg123_init_string(&e->proxyhost);
52 mpg123_init_string(&e->proxyport);
53 }
54
55 void httpdata_reset(struct httpdata *e)
56 {
57 mpg123_free_string(&e->content_type);
58 mpg123_free_string(&e->icy_url);
59 mpg123_free_string(&e->icy_name);
60 e->icy_interval = 0;
61 /* the other stuff shall persist */
62 }
63
64 void httpdata_free(struct httpdata *e)
65 {
66 httpdata_reset(e);
67 mpg123_free_string(&e->proxyhost);
68 mpg123_free_string(&e->proxyport);
69 }
70
71 /* mime type classes */
72 #define M_FILE 0
73 #define M_M3U 1
74 #define M_PLS 2
75 static const char* mime_file[] =
76 {
77 "audio/mpeg", "audio/x-mpeg",
78 "audio/mp3", "audio/x-mp3",
79 "audio/mpeg3", "audio/x-mpeg3",
80 "audio/mpg", "audio/x-mpg",
81 "audio/x-mpegaudio",
82 "application/octet-stream", /* Assume raw binary data is some MPEG data. */
83 NULL
84 };
85 static const char* mime_m3u[] = { "audio/mpegurl", "audio/mpeg-url", "audio/x-mpegurl", NULL };
86 static const char* mime_pls[] =
87 {
88 "audio/x-scpls"
89 , "audio/scpls"
90 , "application/pls"
91 , "application/x-scpls"
92 , "application/pls+xml"
93 , NULL
94 };
95 static const char** mimes[] = { mime_file, mime_m3u, mime_pls, NULL };
96
97 int append_accept(mpg123_string *s)
98 {
99 size_t i,j;
100 if(!mpg123_add_string(s, "Accept: ")) return FALSE;
101
102 /* We prefer what we know. */
103 for(i=0; mimes[i] != NULL; ++i)
104 for(j=0; mimes[i][j] != NULL; ++j)
105 {
106 if( !mpg123_add_string(s, mimes[i][j])
107 || !mpg123_add_string(s, ", ") )
108 return FALSE;
109 }
110 /* Well... in the end, we accept everything, trying to make sense with reality. */
111 if(!mpg123_add_string(s, "*/*")) return FALSE;
112
113 return TRUE;
114 }
115
116 int debunk_mime(const char* mime)
117 {
118 int i,j;
119 size_t len;
120 int r = 0;
121 const char *aux;
122 /* Watch out for such: "audio/x-mpegurl; charset=utf-8" */
123 aux = strchr(mime, ';');
124 if(aux != NULL)
125 {
126 if(!param.quiet)
127 fprintf(stderr, "Warning: additional info in content-type ignored (%s)\n", aux+1);
128 /* Just compare up to before the ";" */
129 len = aux-mime;
130 }
131 /* Else, compare the whole string -- including the end. */
132 else len = strlen(mime)+1;
133
134 /* Skip trailing whitespace, to ne nice to strange folks. */
135 while(len && isspace(mime[len-1])) --len;
136
137 for(i=0; mimes[i] != NULL; ++i)
138 for(j=0; mimes[i][j] != NULL; ++j)
139 if(!strncasecmp(mimes[i][j], mime, len)) goto debunk_result;
140
141 debunk_result:
142 if(mimes[i] != NULL)
143 {
144 switch(i)
145 {
146 case M_FILE: r = IS_FILE; break;
147 case M_M3U: r = IS_LIST|IS_M3U; break;
148 case M_PLS: r = IS_LIST|IS_PLS; break;
149 default: error("unexpected MIME debunk result -- coding error?!");
150 }
151 }
152 return r;
153 }
154
155
156 #ifdef NETWORK
157
158 void encode64 (char *source,char *destination)
159 {
160 static char *Base64Digits =
161 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
162 int n = 0;
163 int ssiz=strlen(source);
164 int i;
165
166 for (i = 0 ; i < ssiz ; i += 3) {
167 unsigned int buf;
168 buf = ((unsigned char *)source)[i] << 16;
169 if (i+1 < ssiz)
170 buf |= ((unsigned char *)source)[i+1] << 8;
171 if (i+2 < ssiz)
172 buf |= ((unsigned char *)source)[i+2];
173
174 destination[n++] = Base64Digits[(buf >> 18) % 64];
175 destination[n++] = Base64Digits[(buf >> 12) % 64];
176 if (i+1 < ssiz)
177 destination[n++] = Base64Digits[(buf >> 6) % 64];
178 else
179 destination[n++] = '=';
180 if (i+2 < ssiz)
181 destination[n++] = Base64Digits[buf % 64];
182 else
183 destination[n++] = '=';
184 }
185 destination[n++] = 0;
186 }
187
188 /* Look out for HTTP header field to parse, construct C string with the value.
189 Attention: Modifies argument, since it's so convenient... */
190 char *get_header_val(const char *hname, mpg123_string *response)
191 {
192 char *tmp = NULL;
193 size_t prelen = strlen(hname);
194 /* if header name found, next char is at least something, so just check for : */
195 if(!strncasecmp(hname, response->p, prelen) && (response->p[prelen] == ':'))
196 {
197 ++prelen;
198 if((tmp = strchr(response->p, '\r')) != NULL ) tmp[0] = 0;
199 if((tmp = strchr(response->p, '\n')) != NULL ) tmp[0] = 0;
200 tmp = response->p+prelen;
201 /* I _know_ that there is a terminating zero, so this loop is safe. */
202 while((tmp[0] == ' ') || (tmp[0] == '\t'))
203 {
204 ++tmp;
205 }
206 }
207 return tmp;
208 }
209
210 /* Iterate over header field names and storage locations, to possibly get those values. */
211 void get_header_string(mpg123_string *response, const char *fieldname, mpg123_string *store)
212 {
213 char *tmp;
214 if((tmp = get_header_val(fieldname, response)))
215 {
216 if(mpg123_set_string(store, tmp)){ debug2("got %s %s", fieldname, store->p); return; }
217 else{ error2("unable to set %s to %s!", fieldname, tmp); }
218 }
219 }
220
221 /* shoutcsast meta data: 1=on, 0=off */
222
223 size_t accept_length(void)
224 {
225 int i,j;
226 static size_t l = 0;
227 if(l) return l;
228 l += strlen("Accept: ");
229 for(i=0; mimes[i] != NULL; ++i)
230 for(j=0; mimes[i][j] != NULL; ++j){ l += strlen(mimes[i][j]) + strlen(", "); }
231 l += strlen("*/*\r\n");
232 debug1("initial computation of accept header length: %lu", (unsigned long)l);
233 return l;
234 }
235
236 /* Returns TRUE or FALSE for success. */
237 int proxy_init(struct httpdata *hd)
238 {
239 int ret = TRUE;
240 /* If we don't have explicit proxy given, probe the environment. */
241 if (!param.proxyurl)
242 if (!(param.proxyurl = getenv("MP3_HTTP_PROXY")))
243 if (!(param.proxyurl = getenv("http_proxy")))
244 param.proxyurl = getenv("HTTP_PROXY");
245 /* Now continue if we have something. */
246 if (param.proxyurl && param.proxyurl[0] && strcmp(param.proxyurl, "none"))
247 {
248 mpg123_string proxyurl;
249 mpg123_init_string(&proxyurl);
250 if( !mpg123_set_string(&proxyurl, param.proxyurl)
251 || !split_url(&proxyurl, NULL, &hd->proxyhost, &hd->proxyport, NULL))
252 {
253 error("splitting proxy URL");
254 ret = FALSE;
255 }
256 else if(param.verbose > 1) fprintf(stderr, "Note: Using proxy %s\n", hd->proxyhost.p);
257 #if 0 /* not yet there */
258 if(!try_host_lookup(proxyhost))
259 {
260 error("Unknown proxy host \"%s\".\n", proxyhost.p);
261 ret = FALSE;
262 }
263 #endif
264 mpg123_free_string(&proxyurl);
265 if(ret) hd->proxystate = PROXY_HOST; /* We got hostname and port settled. */
266 else hd->proxystate = PROXY_NONE;
267 }
268 else hd->proxystate = PROXY_NONE;
269
270 return ret;
271 }
272
273 /*
274 Converts spaces to "%20" ... actually, I have to ask myself why.
275 What about converting them to "+" instead? Would make things a lot easier.
276 Or, on the other hand... what about avoiding HTML encoding at all?
277 */
278 int translate_url(const char *url, mpg123_string *purl)
279 {
280 const char *sptr;
281 /* The length of purl is upper bound by 3*strlen(url) + 1 if
282 * everything in it is a space (%20) - or any encoded character */
283 if (strlen(url) >= SIZE_MAX/3)
284 {
285 error("URL too long. Skipping...");
286 return FALSE;
287 }
288 /* Prepare purl in one chunk, to minimize mallocs. */
289 if(!mpg123_resize_string(purl, strlen(url) + 31)) return FALSE;
290 /*
291 * 2000-10-21:
292 * We would like spaces to be automatically converted to %20's when
293 * fetching via HTTP.
294 * -- Martin Sjoegren <[email protected]>
295 * Hm, why only spaces? Maybe one should do this http stuff more properly...
296 */
297 if ((sptr = strchr(url, ' ')) == NULL)
298 mpg123_set_string(purl, url);
299 else
300 { /* Note that sptr is set from the if to this else... */
301 const char *urlptr = url;
302 mpg123_set_string(purl, "");
303 do {
304 if(! ( mpg123_add_substring(purl, urlptr, 0, sptr-urlptr)
305 && mpg123_add_string(purl, "%20") ) )
306 return FALSE;
307 urlptr = sptr + 1;
308 } while ((sptr = strchr (urlptr, ' ')) != NULL);
309 if(!mpg123_add_string(purl, urlptr)) return FALSE;
310 }
311 /* now see if a terminating / may be needed */
312 if(strchr(purl->p+(strncmp("http://", purl->p, 7) ? 0 : 7), '/') == NULL
313 && !mpg123_add_string(purl, "/"))
314 return FALSE;
315
316 return TRUE;
317 }
318
319 int fill_request(mpg123_string *request, mpg123_string *host, mpg123_string *port, mpg123_string *httpauth1, const char * const *client_head)
320 {
321 char* ttemp;
322 int ret = TRUE;
323
324 /* hm, my test redirection had troubles with line break before HTTP/1.0 */
325 if((ttemp = strchr(request->p,'\r')) != NULL){ *ttemp = 0; request->fill = ttemp-request->p+1; }
326
327 if((ttemp = strchr(request->p,'\n')) != NULL){ *ttemp = 0; request->fill = ttemp-request->p+1; }
328
329 /* Fill out the request further... */
330 if( !mpg123_add_string(request, " HTTP/1.0\r\nUser-Agent: ")
331 || !mpg123_add_string(request, PACKAGE_NAME)
332 || !mpg123_add_string(request, "/")
333 || !mpg123_add_string(request, MPG123_VERSION)
334 || !mpg123_add_string(request, "\r\n") )
335 return FALSE;
336
337 if(host->fill)
338 { /* Give virtual hosting a chance... adding the "Host: ... " line. */
339 debug2("Host: %s:%s", host->p, port->p);
340 if(!( mpg123_add_string(request, "Host: ")
341 && mpg123_add_string(request, host->p)
342 && mpg123_add_string(request, ":")
343 && mpg123_add_string(request, port->p)
344 && mpg123_add_string(request, "\r\n") ))
345 return FALSE;
346 }
347
348 /* Authorization. */
349 if (httpauth1->fill || param.httpauth) {
350 char *buf;
351 if(!mpg123_add_string(request,"Authorization: Basic ")) return FALSE;
352 if(httpauth1->fill) {
353 if(httpauth1->fill > SIZE_MAX / 4) return FALSE;
354
355 buf=(char *)malloc(httpauth1->fill * 4);
356 if(!buf)
357 {
358 error("malloc() failed for http auth, out of memory.");
359 return FALSE;
360 }
361 encode64(httpauth1->p,buf);
362 } else {
363 if(strlen(param.httpauth) > SIZE_MAX / 4 - 4 ) return FALSE;
364
365 buf=(char *)malloc((strlen(param.httpauth) + 1) * 4);
366 if(!buf)
367 {
368 error("malloc() for http auth failed, out of memory.");
369 return FALSE;
370 }
371 encode64(param.httpauth,buf);
372 }
373
374 if( !mpg123_add_string(request, buf) || !mpg123_add_string(request, "\r\n"))
375 ret = FALSE;
376
377 free(buf); /* Watch out for leaking if you introduce returns before this line. */
378 }
379 while(ret && *client_head)
380 {
381 ret = mpg123_add_string(request, *client_head);
382 if(ret)
383 ret = mpg123_add_string(request, "\r\n");
384 ++client_head;
385 }
386 if(ret) ret = mpg123_add_string(request, "\r\n");
387
388 return ret;
389 }
390
391 int http_open(const char* url, struct httpdata *hd, const char * const *client_head)
392 {
393 mpg123_string purl, host, port, path;
394 mpg123_string request, request_url;
395 mpg123_string httpauth1;
396 int sock = -1;
397 int oom = 0;
398 mpg123_init_string(&purl);
399 mpg123_init_string(&host);
400 mpg123_init_string(&port);
401 mpg123_init_string(&path);
402 mpg123_init_string(&request);
403 mpg123_init_string(&request_url);
404 mpg123_init_string(&httpauth1);
405
406 /* Get initial info for proxy server. Once. */
407 if(hd->proxystate == PROXY_UNKNOWN && !proxy_init(hd)) goto exit;
408
409 if(!translate_url(url, &purl)){ oom=1; goto exit; }
410
411 /* Don't confuse the different auth strings... */
412 if(!split_url(&purl, &httpauth1, NULL, NULL, NULL) ){ oom=1; goto exit; }
413
414 {
415 /* Storing the request url, with http:// prepended if needed. */
416 /* used to be url here... seemed wrong to me (when loop advanced...) */
417 if(strncasecmp(purl.p, "http://", 7) != 0) mpg123_set_string(&request_url, "http://");
418 else mpg123_set_string(&request_url, "");
419
420 mpg123_chomp_string(&purl);
421 mpg123_add_string(&request_url, purl.p);
422
423 /* Always store the host and port from the URL for correct host header
424 in the request. Proxy server is used for connection, but never in the
425 host header! */
426 if(!split_url(&purl, NULL, &host, &port, &path)){ oom=1; goto exit; }
427 if (hd->proxystate >= PROXY_HOST)
428 {
429 /* We will connect to proxy, full URL goes into the request. */
430 if( !mpg123_set_string(&request, "GET ")
431 || !mpg123_add_string(&request, request_url.p) )
432 {
433 oom=1; goto exit;
434 }
435 }
436 else
437 {
438 /* We will connect to the host from the URL and only the path goes into the request. */
439 if( !mpg123_set_string(&request, "GET ")
440 || !mpg123_add_string(&request, path.p) )
441 {
442 oom=1; goto exit;
443 }
444 }
445
446 if(!fill_request(&request, &host, &port, &httpauth1, client_head)){ oom=1; goto exit; }
447
448 httpauth1.fill = 0; /* We use the auth data from the URL only once. */
449 if (hd->proxystate >= PROXY_HOST)
450 {
451 /* Only the host:port used for actual connection is replaced by
452 proxy. */
453 if( !mpg123_copy_string(&hd->proxyhost, &host)
454 || !mpg123_copy_string(&hd->proxyport, &port) )
455 {
456 oom=1; goto exit;
457 }
458 }
459 debug2("attempting to open_connection to %s:%s", host.p, port.p);
460 #ifdef WANT_WIN32_SOCKETS
461 sock = win32_net_open_connection(&host, &port);
462 #else
463 sock = open_connection(&host, &port);
464 #endif
465 if(sock < 0)
466 {
467 error1("Unable to establish connection to %s", host.fill ? host.p : "");
468 goto exit;
469 }
470 #define http_failure close(sock); sock=-1; goto exit;
471
472 if(param.verbose > 2) fprintf(stderr, "HTTP request:\n%s\n",request.p);
473 #ifdef WANT_WIN32_SOCKETS
474 if(!win32_net_writestring (sock, &request))
475 #else
476 if(INT123_unintr_write(sock, request.p, request.fill-1) != request.fill-1)
477 #endif
478 {
479 http_failure;
480 }
481 }
482
483 exit: /* The end as well as the exception handling point... */
484 if(oom) error("Apparently, I ran out of memory or had some bad input data...");
485
486 mpg123_free_string(&purl);
487 mpg123_free_string(&host);
488 mpg123_free_string(&port);
489 mpg123_free_string(&path);
490 mpg123_free_string(&request);
491 mpg123_free_string(&request_url);
492 mpg123_free_string(&httpauth1);
493 return sock;
494 }
495 #endif
496
497 /* EOF */
498