Debian Codegraph @

src/streamdump.c

raw
1 /*
2 streamdump: Dumping a copy of the input data.
3
4 This evolved into the generic I/O interposer for direct file or http stream
5 access, with explicit buffering for getline.
6
7 copyright 2010-2023 by the mpg123 project - free software under the terms of the LGPL 2.1
8 see COPYING and AUTHORS files in distribution or http://mpg123.org
9 initially written by Michael Hipp
10 */
11
12 // setenv
13 #define _XOPEN_SOURCE 600
14 #define _POSIX_C_SOURCE 200112L
15
16 #include "streamdump.h"
17 #include <fcntl.h>
18 #include <errno.h>
19 #include "common/debug.h"
20
21 #ifndef O_BINARY
22 #define O_BINARY 0
23 #endif
24
25 #ifndef HTTP_MAX_RELOCATIONS
26 #define HTTP_MAX_RELOCATIONS 20
27 #endif
28
29 #if defined(NETWORK) && !defined(NET123)
30 #error "NETWORK only with NET123 from now on!"
31 #endif
32 #if !defined(NETWORK) && defined(NET123)
33 #error "NET123 only with NETWORK from now on!"
34 #endif
35
36
37 #ifdef NETWORK
38
39 const char *net123_backends[] =
40 {
41 "internal"
42 #ifdef NET123_EXEC
43 , "wget"
44 , "curl"
45 #endif
46 #ifdef NET123_WINHTTP
47 , "winhttp"
48 #endif
49 #ifdef NET123_WININET
50 , "wininet"
51 #endif
52 , NULL
53 };
54
55 // Net123 variant for our internal code that has safer legacy support for HTTP shoutcast.
56
57 static size_t net123_read_internal( struct net123_handle_struct *nh,
58 void *buf, size_t bufsize )
59 {
60 if(!nh)
61 return 0;
62 int *fdp = nh->parts;
63 #ifdef WANT_WIN32_SOCKETS
64 return win32_net_read(*fdp, buf, bufsize);
65 #else
66 return INT123_unintr_read(*fdp, buf, bufsize);
67 #endif
68
69 }
70
71 static void net123_close_internal(struct net123_handle_struct *nh)
72 {
73 if(!nh)
74 return;
75 int *fdp = nh->parts;
76 #ifdef WANT_WIN32_SOCKETS
77 if(*fdp != SOCKET_ERROR)
78 win32_net_close(*fdp);
79 #else
80 close(*fdp);
81 #endif
82 free(fdp);
83 free(nh);
84 }
85
86 static net123_handle *net123_open_internal( const char *url
87 , const char * const *client_head, struct httpdata *hd )
88 {
89 net123_handle *nh = malloc(sizeof(net123_handle));
90 if(!nh)
91 return NULL;
92 int *fdp = malloc(sizeof(int));
93 if(!fdp)
94 {
95 free(nh);
96 return NULL;
97 }
98 nh->parts = fdp;
99 nh->read = net123_read_internal;
100 nh->close = net123_close_internal;
101 // Handles win32_net internally.
102 *fdp = http_open(url, hd, client_head);
103 if(*fdp >= 0)
104 return nh;
105 free(fdp);
106 free(nh);
107 return NULL;
108 }
109
110 // Decide which backend to load.
111 static net123_handle *net123_open( const char *url
112 , const char * const *client_head, struct httpdata *hd )
113 {
114 int autochoose = !strcmp("auto", param.network_backend);
115 int https = !strncasecmp("https://", url, 8);
116 if(param.proxyurl)
117 {
118 if(strcmp(param.proxyurl, "none"))
119 {
120 #ifdef HAVE_SETENV
121 setenv("http_proxy", param.proxyurl, 1);
122 setenv("HTTP_PROXY", param.proxyurl, 1);
123 setenv("https_proxy", param.proxyurl, 1);
124 setenv("HTTPS_PROXY", param.proxyurl, 1);
125 #endif
126 } else
127 {
128 #ifdef HAVE_UNSETENV
129 unsetenv("http_proxy");
130 unsetenv("HTTP_PROXY");
131 unsetenv("https_proxy");
132 unsetenv("HTTPS_PROXY");
133 #endif
134 }
135 }
136 if( (autochoose && !https)
137 || !strcmp("internal", param.network_backend) )
138 {
139 if(https && !param.quiet)
140 fprintf(stderr, "Note: HTTPS will fail with internal network code.\n");
141 return net123_open_internal(url, client_head, hd);
142 }
143 #ifdef NET123_EXEC
144 if( autochoose
145 || !strcmp("wget", param.network_backend)
146 || !strcmp("curl", param.network_backend) )
147 return net123_open_exec(url, client_head);
148 #endif
149 #ifdef NET123_WININET
150 if(autochoose || !strcmp("wininet", param.network_backend))
151 return net123_open_wininet(url, client_head);
152 #endif
153 #ifdef NET123_WINHTTP
154 if(autochoose || !strcmp("winhttp", param.network_backend))
155 return net123_open_winhttp(url, client_head);
156 #endif
157 merror("no network backend for %s", https ? "HTTPS" : "HTTP");
158 return NULL;
159 }
160 #endif
161
162 /* Stream dump descriptor. */
163 static int dump_fd = -1;
164
165 // Read without the buffer. This is used to fill the buffer explicitly in getline.
166 // This is the function that finally wraps around all the different types of input.
167
168 static mpg123_ssize_t stream_read_raw(struct stream *sd, void *buf, size_t count)
169 {
170 mpg123_ssize_t ret = -1;
171 #ifdef NETWORK
172 if(sd->nh)
173 ret = (mpg123_ssize_t) sd->nh->read(sd->nh, buf, count);
174 #endif
175 if(sd->fd >= 0) // plain file or network socket
176 ret = (mpg123_ssize_t) INT123_unintr_read(sd->fd, buf, count);
177 return ret;
178 }
179
180 static mpg123_ssize_t stream_read(struct stream *sd, void *buf, size_t count)
181 {
182 if(!sd)
183 return -1;
184 char *bbuf = buf;
185 mpg123_ssize_t ret = 0;
186 if(count > SSIZE_MAX)
187 return -1;
188 while(count)
189 {
190 size_t get = 0;
191 if(sd->fill)
192 { // drain the buffer
193 get = sd->fill > count ? count : sd->fill;
194 memcpy(bbuf, sd->bufp, get);
195 sd->fill -= get;
196 sd->bufp += get;
197 } else
198 { // get it from the source
199 mpg123_ssize_t rret = stream_read_raw(sd, bbuf, count);
200 if(rret < 0)
201 return ret > 0 ? ret : -1;
202 if(rret == 0)
203 return ret;
204 get = rret;
205 }
206 bbuf += get;
207 count -= get;
208 ret += get;
209 }
210 return ret;
211 }
212
213 static off_t stream_seek(struct stream *sd, off_t pos, int whence)
214 {
215 if(!sd)
216 return -1;
217 #ifdef NET123
218 if(sd->nh)
219 return -1;
220 #endif
221 return lseek(sd->fd, pos, whence);
222 }
223
224 // Read into the stream buffer, look for line endings there, copy stuff.
225 // This should work with \n and \r\n sequences ... even just \r sequences?
226 // Yes, either \r or \n ends a line, a following \n or \r is just swallowed.
227 // Need to catch the case where the buffer ends with \r and the next buffer
228 // contents start with the matching \n, and the other way round.
229 mpg123_ssize_t stream_getline(struct stream *sd, mpg123_string *line)
230 {
231 if(!sd || !line)
232 return -1;
233 line->fill = 0; // this is EOF
234 char lend = 0;
235 while(1)
236 {
237 mdebug("getline loop with %d", sd->fill);
238 // If we got just an \r, we need to ensure that we swalloed the matching \n,
239 // too. This implies that we got a line stored already.
240 if(sd->fill && lend)
241 {
242 // finish skipping over an earlier line end
243 if( (*sd->bufp == '\n' || *sd->bufp == '\r') && *sd->bufp != lend)
244 {
245 ++sd->bufp;
246 --sd->fill;
247 }
248 // whatever happened, no half-line-end lurking anymore
249 return line->fill;
250 }
251 if(sd->fill)
252 {
253 // look for line end here, copy things
254 size_t i = 0;
255 while(i < sd->fill && sd->bufp[i] != '\n' && sd->bufp[i] != '\r')
256 ++i;
257 // either found an end, or hit the end
258 if(!mpg123_add_substring(line, sd->bufp, 0, i))
259 return -1; // out of memory
260 // skip over a line end if found
261 if(i == sd->fill)
262 {
263 // not done yet, refill, please
264 sd->fill = 0;
265 } else
266 {
267 // got end and stored complete line, but need to go on to capture full line end
268 lend = sd->bufp[i]; // either \r or \n
269 sd->bufp += i+1;
270 sd->fill -= i+1;
271 }
272 } else
273 {
274 debug("re-filling buffer");
275 // refill buffer
276 mpg123_ssize_t ret = stream_read_raw(sd, sd->buf, sizeof(sd->buf));
277 mdebug("raw read return: %zd", ret);
278 if(ret < 0)
279 return -1;
280 else if(ret == 0)
281 return line->fill; // A line ends at end of file.
282 else
283 {
284 sd->fill = ret;
285 sd->bufp = sd->buf;
286 }
287 }
288 }
289 }
290
291 #ifdef NETWORK
292 // Return 0 on success, non-zero when there is an error or more work to do.
293 // -1: error, 1: redirection to given location
294 static int stream_parse_headers(struct stream *sd, mpg123_string *location)
295 {
296 int ret = 0;
297 mpg123_string line;
298 mpg123_init_string(&line);
299 mpg123_string icyint;
300 mpg123_init_string(&icyint);
301 int redirect = 0;
302 location->fill = 0;
303 const char *head[] = { "content-type", "icy-name"
304 , "icy-url", "icy-metaint", "location" };
305 mpg123_string *val[] = { &sd->htd.content_type, &sd->htd.icy_name
306 , &sd->htd.icy_url, &icyint, location };
307 int hn = sizeof(head)/sizeof(char*);
308 int hi = -1;
309 int got_ok = 0;
310 int got_line = 0;
311 debug("parsing headers");
312 while(stream_getline(sd, &line) > 0)
313 {
314 got_line = 1;
315 mdebug("HTTP in: %s", line.p);
316 if(line.p[0] == 0)
317 {
318 break; // This is the content separator line.
319 }
320 { // Convert from unknown/ASCII encoding to UTF-8. Play safe.
321 char *buf = NULL;
322 if(unknown2utf8(&buf, line.p, -1))
323 {
324 error("failed converting HTTP header line");
325 continue;
326 }
327 // Avoiding extra allocation here would be nice. mpg123_adopt_string()?
328 mpg123_set_string(&line, buf);
329 free(buf);
330 }
331 // React to HTTP error codes, but do not enforce an OK being sent as Shoutcast
332 // only produces very minimal headers, not even a HTTP response code.
333 // Well, ICY 200 OK could be there, but then we got other headers to know
334 // things are fine.
335 if(!strncasecmp("http/", line.p, 5))
336 {
337 // HTTP/1.1 200 OK
338 char *tok = line.p;
339 while(*tok && *tok != ' ' && *tok != '\t')
340 ++tok;
341 while(*tok && (*tok == ' ' || *tok == '\t'))
342 ++tok;
343 if(tok && *tok != '2')
344 {
345 if(*tok == '3')
346 {
347 redirect = ret = 1;
348 if(param.verbose > 2)
349 fprintf(stderr, "Note: HTTP redirect\n");
350 } else
351 {
352 merror("HTTP error response: %s", line.p);
353 ret = -1;
354 break;
355 }
356 } else if(tok && *tok == '2')
357 {
358 if(param.verbose > 2)
359 fprintf(stderr, "Note: got a positive HTTP response\n");
360 got_ok = 1;
361 }
362 }
363 if(hi >= 0 && (line.p[0] == ' ' || line.p[0] == '\t'))
364 {
365 debug("header continuation");
366 // nh continuation line, appending to already stored value.
367 char *v = line.p+1;
368 while(*v == ' ' || *v == '\t'){ ++v; }
369 if(!mpg123_add_string(val[hi], v))
370 {
371 merror("failed to grow header value for %s", head[hi]);
372 hi = -1;
373 continue;
374 }
375 }
376 char *n = line.p;
377 char *v = strchr(line.p, ':');
378 if(!v)
379 continue; // No proper header line.
380 // Got a header line.
381 *v = 0; // Terminate the header name.
382 if(param.verbose > 2)
383 fprintf(stderr, "Note: got header: %s\n", n);
384 ++v; // Value starts after : and whitespace.
385 while(*v == ' ' || *v == '\t'){ ++v; }
386 for(hi = 0; hi<hn; ++hi)
387 {
388 if(!strcasecmp(n, head[hi]))
389 break;
390 }
391 if(hi == hn)
392 {
393 debug("skipping uninteresting header");
394 hi = -1;
395 continue;
396 }
397 if(param.verbose > 2)
398 fprintf(stderr, "Note: storing HTTP header %s: %s\n", head[hi], v);
399 got_ok = 1; // When we got some header to store, things seem fine.
400 if(!mpg123_set_string(val[hi], v))
401 {
402 error("failed to allocate header value storage");
403 hi = -1;
404 continue;
405 }
406 }
407 if(!redirect && icyint.fill)
408 {
409 sd->htd.icy_interval = atol(icyint.p);
410 if(param.verbose > 1)
411 fprintf(stderr, "Info: ICY interval %li\n", (long)sd->htd.icy_interval);
412 }
413 if(!got_line)
414 {
415 error("no data at all from network resource");
416 ret = -1;
417 } else if(redirect && !location->fill)
418 {
419 error("redirect but no location given");
420 } else if(!got_ok)
421 {
422 error("missing positive server response");
423 ret = -1;
424 }
425 mpg123_free_string(&icyint);
426 mpg123_free_string(&line);
427 return ret;
428 }
429
430 // resolve relative locations given the initial full URL
431 // full URL ensured to either start with http:// or https://
432 // (case-insensitive), location non-empty
433 static void relocate_url(mpg123_string *location, const char *url)
434 {
435 if(!strncasecmp(location->p, "http://", 7) || !strncasecmp(location->p, "https://", 8))
436 return;
437 if(!url || (strncasecmp(url, "http://", 7) && strncasecmp(url, "https://", 8)))
438 {
439 mpg123_resize_string(location, 0);
440 return;
441 }
442
443 if(!param.quiet)
444 fprintf(stderr, "NOTE: no complete URL in redirect, constructing one\n");
445
446 mpg123_string purl;
447 mpg123_string workbuf;
448 mpg123_init_string(&purl);
449 mpg123_init_string(&workbuf);
450
451 if(mpg123_set_string(&purl, url) && mpg123_move_string(location, &workbuf))
452 {
453 debug1("relocate request_url: %s", purl.p);
454 // location somewhat relative, either /some/path or even just some/path
455 char* ptmp = NULL;
456 // Though it's not RFC (?), accept relative URIs as wget does.
457 if(workbuf.p[0] == '/')
458 {
459 // server-absolute only prepend http://server/
460 // I null the first / after http:// or https://
461 size_t off = (purl.p[4] == 's') ? 8 : 7;
462 ptmp = strchr(purl.p+off,'/');
463 if(ptmp != NULL)
464 {
465 purl.fill = ptmp-purl.p+1;
466 purl.p[purl.fill-1] = 0;
467 }
468 }
469 else
470 {
471 // relative to current directory
472 // prepend http://server/path/
473 // first cutting off parameter stuff from URL
474 for(size_t i=0; i<purl.fill; ++i)
475 {
476 if(purl.p[i] == '?' || purl.p[i] == '#')
477 {
478 purl.p[i] = 0;
479 purl.fill = i+1;
480 break;
481 }
482 }
483 // now the last slash, keeping it
484 ptmp = strrchr(purl.p, '/');
485 if(ptmp != NULL)
486 {
487 purl.fill = ptmp-purl.p+2;
488 purl.p[purl.fill-1] = 0;
489 }
490 }
491 // only the prefix left
492 debug1("prefix=%s", purl.p);
493 mpg123_add_string(location, purl.p);
494 }
495
496 mpg123_add_string(location, workbuf.p);
497
498 mpg123_free_string(&workbuf);
499 mpg123_free_string(&purl);
500 }
501
502 #endif
503
504 static void stream_init(struct stream *sd)
505 {
506 sd->bufp = sd->buf;
507 sd->fill = 0;
508 sd->fd = -1;
509 #ifdef NET123
510 sd->nh = NULL;
511 #endif
512 httpdata_init(&sd->htd);
513 }
514
515 // Clean up things for another connection.
516 // Does not reset the network flag.
517 static void stream_reset(struct stream *sd)
518 {
519 #ifdef NET123
520 if(sd->nh)
521 sd->nh->close(sd->nh);
522 sd->nh = NULL;
523 #endif
524 if(sd->fd >= 0) // plain file or network socket
525 close(sd->fd);
526 sd->fd = -1;
527 httpdata_free(&sd->htd);
528 httpdata_init(&sd->htd);
529 sd->bufp = sd->buf;
530 sd->fill = 0;
531 }
532
533 struct stream *stream_open(const char *url)
534 {
535 struct stream *sd = malloc(sizeof(struct stream));
536 if(!sd)
537 return NULL;
538 stream_init(sd);
539 mdebug("opening resource %s", url);
540 if(!strcmp(url, "-"))
541 {
542 sd->fd = STDIN_FILENO;
543 INT123_compat_binmode(STDIN_FILENO, TRUE);
544 }
545 #ifdef NETWORK
546 else if(!strncasecmp("http://", url, 7) || !strncasecmp("https://", url, 8))
547 {
548 // Network stream with header parsing.
549 const char *client_head[] = { NULL, NULL, NULL };
550 client_head[0] = param.talk_icy ? icy_yes : icy_no;
551 mpg123_string accept;
552 mpg123_init_string(&accept);
553 append_accept(&accept);
554 client_head[1] = accept.p;
555 mpg123_string location;
556 mpg123_string urlcopy;
557 mpg123_init_string(&location);
558 mpg123_init_string(&urlcopy);
559 int numrelocs = 0;
560 while(sd)
561 {
562 sd->nh = net123_open(url, client_head, &sd->htd);
563 if(!sd->nh)
564 {
565 stream_close(sd);
566 sd = NULL;
567 break;
568 }
569 location.fill = 0;
570 if(stream_parse_headers(sd, &location))
571 {
572 stream_reset(sd);
573 if(location.fill)
574 {
575 if(++numrelocs > HTTP_MAX_RELOCATIONS)
576 {
577 merror("too many HTTP redirections: %i", numrelocs);
578 url = NULL;
579 } else
580 {
581 relocate_url(&location, url); // resolve relative locations
582 mpg123_copy_string(&location, &urlcopy);
583 url = urlcopy.p;
584 }
585 } else
586 {
587 url = NULL;
588 }
589 if(!url)
590 {
591 stream_close(sd);
592 sd = NULL;
593 }
594 } else break; // Successful end.
595 }
596 mpg123_free_string(&urlcopy);
597 mpg123_free_string(&location);
598 mpg123_free_string(&accept);
599 // Either sd is NULL or we got a stream ready.
600 }
601 #endif
602 else
603 {
604 // plain file access
605 if(!strncasecmp("file://", url, 7))
606 url+= 7; // Might be useful to prepend file scheme prefix for local stuff.
607 errno = 0;
608 sd->fd = INT123_compat_open(url, O_RDONLY|O_BINARY);
609 if(sd->fd < 0)
610 {
611 merror("failed to open file: %s: %s", url, INT123_strerror(errno));
612 stream_close(sd);
613 return NULL;
614 }
615 }
616 return sd;
617 }
618
619 void stream_close(struct stream *sd)
620 {
621 if(!sd)
622 return;
623 stream_reset(sd);
624 free(sd);
625 }
626
627 /* Read data from input, write copy to dump file. */
628 static mpg123_ssize_t dump_read(void *handle, void *buf, size_t count)
629 {
630 struct stream *sd = handle;
631 mpg123_ssize_t ret = stream_read(sd, buf, count);
632 if(ret > 0 && dump_fd > -1)
633 {
634 ret = INT123_unintr_write(dump_fd, buf, ret);
635 }
636 return ret;
637 }
638
639 /* Also mirror seeks, to prevent messed up dumps of seekable streams. */
640 static off_t dump_seek(void *handle, off_t pos, int whence)
641 {
642 struct stream *sd = handle;
643 off_t ret = stream_seek(sd, pos, whence);
644 if(ret >= 0 && dump_fd > -1)
645 {
646 ret = lseek(dump_fd, pos, whence);
647 }
648 return ret;
649 }
650
651 /* External API... open and close. */
652 int dump_setup(struct stream *sd, mpg123_handle *mh)
653 {
654 int ret = MPG123_OK;
655 int do_replace = 0; // full replacement with handle
656
657 // paranoia: if buffer active, ensure handle I/O
658 if(sd->fill)
659 do_replace = 1;
660 #ifdef NET123
661 if(sd->nh)
662 do_replace = 1;
663 #endif
664 if(param.streamdump)
665 {
666 do_replace = 1;
667 // open freshly or keep open
668 if(dump_fd < 0)
669 {
670 if(!param.quiet)
671 fprintf(stderr, "Note: Dumping stream to %s\n", param.streamdump);
672 dump_fd = INT123_compat_open(param.streamdump, O_CREAT|O_TRUNC|O_RDWR);
673 }
674 if(dump_fd < 0)
675 {
676 error1("Failed to open dump file: %s\n", INT123_strerror(errno));
677 return -1;
678 }
679 #ifdef WIN32
680 _setmode(dump_fd, _O_BINARY);
681 #endif
682 }
683
684 if( MPG123_OK != mpg123_param(mh, MPG123_ICY_INTERVAL
685 , param.icy_interval ? param.icy_interval : sd->htd.icy_interval, 0) )
686 error1("Cannot set ICY interval: %s", mpg123_strerror(mh));
687 if(param.icy_interval > 0 && param.verbose > 1)
688 fprintf(stderr, "Info: Forced ICY interval %li\n", param.icy_interval);
689
690 if(do_replace)
691 {
692 mpg123_replace_reader_handle(mh, dump_read, dump_seek, NULL);
693 ret = mpg123_open_handle(mh, sd);
694 } else
695 {
696 mpg123_replace_reader(mh, NULL, NULL);
697 ret = mpg123_open_fd(mh, sd->fd);
698 }
699 if(ret != MPG123_OK)
700 {
701 error1("Unable to replace reader/open track for stream dump: %s\n", mpg123_strerror(mh));
702 dump_close();
703 return -1;
704 }
705 else return 0;
706 }
707
708 void dump_close(void)
709 {
710 if(dump_fd > -1) INT123_compat_close(dump_fd);
711
712 dump_fd = -1;
713 }