DOCS/xml: change --with-extraincdir to --extra-cflags
[mplayer/glamo.git] / stream / http.c
blob9d00f00f872679719b96428d440eb5da9ebf4835
1 /*
2 * HTTP Helper
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #if !HAVE_WINSOCK2_H
31 #else
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 #endif
36 #include "http.h"
37 #include "url.h"
38 #include "mp_msg.h"
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "network.h"
45 extern int stream_cache_size;
46 extern int network_bandwidth;
48 typedef struct {
49 unsigned metaint;
50 unsigned metapos;
51 int is_ultravox;
52 } scast_data_t;
54 /**
55 * \brief first read any data from sc->buffer then from fd
56 * \param fd file descriptor to read data from
57 * \param buffer buffer to read into
58 * \param len how many bytes to read
59 * \param sc streaming control containing buffer to read from first
60 * \return len unless there is a read error or eof
62 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
63 unsigned pos = 0;
64 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
65 if (cp_len > len)
66 cp_len = len;
67 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
68 sc->buffer_pos += cp_len;
69 pos += cp_len;
70 while (pos < len) {
71 int ret = recv(fd, &buffer[pos], len - pos, 0);
72 if (ret <= 0)
73 break;
74 pos += ret;
76 return pos;
79 /**
80 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
81 * \param fd file descriptor to read from
82 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
83 * \return number of real data before next metadata block starts or 0 on error
85 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
86 unsigned metaint;
87 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
88 int info_read;
89 do {
90 info_read = my_read(fd, info, 1, sc);
91 if (info[0] == 0x00)
92 info_read = my_read(fd, info, 6, sc);
93 else
94 info_read += my_read(fd, &info[1], 5, sc);
95 if (info_read != 6) // read error or eof
96 return 0;
97 // sync byte and reserved flags
98 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
99 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
100 return 0;
102 if (info[1] & 0x01)
103 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
104 metaint = info[4] << 8 | info[5];
105 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
106 char *metabuf = malloc(metaint);
107 my_read(fd, metabuf, metaint, sc);
108 free(metabuf);
110 } while ((info[3] & 0xf) < 0x07);
111 return metaint;
115 * \brief read one scast meta data entry and print it
116 * \param fd file descriptor to read from
117 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
119 static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
120 unsigned char tmp = 0;
121 unsigned metalen;
122 my_read(fd, &tmp, 1, sc);
123 metalen = tmp * 16;
124 if (metalen > 0) {
125 int i;
126 uint8_t *info = malloc(metalen + 1);
127 unsigned nlen = my_read(fd, info, metalen, sc);
128 // avoid breaking the user's terminal too much
129 if (nlen > 256) nlen = 256;
130 for (i = 0; i < nlen; i++)
131 if (info[i] && info[i] < 32) info[i] = '?';
132 info[nlen] = 0;
133 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
134 free(info);
139 * \brief read data from scast/ultravox stream without any metadata
140 * \param fd file descriptor to read from
141 * \param buffer buffer to read data into
142 * \param size number of bytes to read
143 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
145 static int scast_streaming_read(int fd, char *buffer, int size,
146 streaming_ctrl_t *sc) {
147 scast_data_t *sd = (scast_data_t *)sc->data;
148 unsigned block, ret;
149 unsigned done = 0;
151 // first read remaining data up to next metadata
152 block = sd->metaint - sd->metapos;
153 if (block > size)
154 block = size;
155 ret = my_read(fd, buffer, block, sc);
156 sd->metapos += ret;
157 done += ret;
158 if (ret != block) // read problems or eof
159 size = done;
161 while (done < size) { // now comes the metadata
162 if (sd->is_ultravox)
164 sd->metaint = uvox_meta_read(fd, sc);
165 if (!sd->metaint)
166 size = done;
168 else
169 scast_meta_read(fd, sc); // read and display metadata
170 sd->metapos = 0;
171 block = size - done;
172 if (block > sd->metaint)
173 block = sd->metaint;
174 ret = my_read(fd, &buffer[done], block, sc);
175 sd->metapos += ret;
176 done += ret;
177 if (ret != block) // read problems or eof
178 size = done;
180 return done;
183 static int scast_streaming_start(stream_t *stream) {
184 int metaint;
185 scast_data_t *scast_data;
186 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
187 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
188 if (!stream || stream->fd < 0 || !http_hdr)
189 return -1;
190 if (is_ultravox)
191 metaint = 0;
192 else {
193 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
194 if (metaint <= 0)
195 return -1;
197 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
198 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
199 stream->streaming_ctrl->buffer_pos = 0;
200 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
201 scast_data = malloc(sizeof(scast_data_t));
202 scast_data->metaint = metaint;
203 scast_data->metapos = 0;
204 scast_data->is_ultravox = is_ultravox;
205 http_free(http_hdr);
206 stream->streaming_ctrl->data = scast_data;
207 stream->streaming_ctrl->streaming_read = scast_streaming_read;
208 stream->streaming_ctrl->streaming_seek = NULL;
209 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
210 stream->streaming_ctrl->buffering = 1;
211 stream->streaming_ctrl->status = streaming_playing_e;
212 return 0;
215 static int nop_streaming_start( stream_t *stream ) {
216 HTTP_header_t *http_hdr = NULL;
217 char *next_url=NULL;
218 URL_t *rd_url=NULL;
219 int fd,ret;
220 if( stream==NULL ) return -1;
222 fd = stream->fd;
223 if( fd<0 ) {
224 fd = http_send_request( stream->streaming_ctrl->url, 0 );
225 if( fd<0 ) return -1;
226 http_hdr = http_read_response( fd );
227 if( http_hdr==NULL ) return -1;
229 switch( http_hdr->status_code ) {
230 case 200: // OK
231 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
232 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
233 if( http_hdr->body_size>0 ) {
234 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
235 http_free( http_hdr );
236 return -1;
239 break;
240 // Redirect
241 case 301: // Permanently
242 case 302: // Temporarily
243 case 303: // See Other
244 ret=-1;
245 next_url = http_get_field( http_hdr, "Location" );
247 if (next_url != NULL)
248 rd_url=url_new(next_url);
250 if (next_url != NULL && rd_url != NULL) {
251 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
252 stream->streaming_ctrl->url=check4proxies(rd_url);
253 ret=nop_streaming_start(stream); //recursively get streaming started
254 } else {
255 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
256 closesocket( fd );
257 fd = -1;
259 return ret;
260 break;
261 case 401: //Authorization required
262 case 403: //Forbidden
263 case 404: //Not found
264 case 500: //Server Error
265 default:
266 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
267 closesocket( fd );
268 fd = -1;
269 return -1;
270 break;
272 stream->fd = fd;
273 } else {
274 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
275 if( http_hdr->body_size>0 ) {
276 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
277 http_free( http_hdr );
278 stream->streaming_ctrl->data = NULL;
279 return -1;
284 if( http_hdr ) {
285 http_free( http_hdr );
286 stream->streaming_ctrl->data = NULL;
289 stream->streaming_ctrl->streaming_read = nop_streaming_read;
290 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
291 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
292 stream->streaming_ctrl->buffering = 1;
293 stream->streaming_ctrl->status = streaming_playing_e;
294 return 0;
297 HTTP_header_t *
298 http_new_header(void) {
299 HTTP_header_t *http_hdr;
301 http_hdr = malloc(sizeof(HTTP_header_t));
302 if( http_hdr==NULL ) return NULL;
303 memset( http_hdr, 0, sizeof(HTTP_header_t) );
305 return http_hdr;
308 void
309 http_free( HTTP_header_t *http_hdr ) {
310 HTTP_field_t *field, *field2free;
311 if( http_hdr==NULL ) return;
312 free(http_hdr->protocol);
313 free(http_hdr->uri);
314 free(http_hdr->reason_phrase);
315 free(http_hdr->field_search);
316 free(http_hdr->method);
317 free(http_hdr->buffer);
318 field = http_hdr->first_field;
319 while( field!=NULL ) {
320 field2free = field;
321 free(field->field_name);
322 field = field->next;
323 free( field2free );
325 free( http_hdr );
326 http_hdr = NULL;
330 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
331 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
333 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
334 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
335 return -1;
337 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
338 if( http_hdr->buffer==NULL ) {
339 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
340 return -1;
342 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
343 http_hdr->buffer_size += length;
344 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
345 return http_hdr->buffer_size;
349 http_is_header_entire( HTTP_header_t *http_hdr ) {
350 if( http_hdr==NULL ) return -1;
351 if( http_hdr->buffer==NULL ) return 0; // empty
353 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
354 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
355 return 1;
359 http_response_parse( HTTP_header_t *http_hdr ) {
360 char *hdr_ptr, *ptr;
361 char *field=NULL;
362 int pos_hdr_sep, hdr_sep_len;
363 size_t len;
364 if( http_hdr==NULL ) return -1;
365 if( http_hdr->is_parsed ) return 0;
367 // Get the protocol
368 hdr_ptr = strstr( http_hdr->buffer, " " );
369 if( hdr_ptr==NULL ) {
370 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
371 return -1;
373 len = hdr_ptr-http_hdr->buffer;
374 http_hdr->protocol = malloc(len+1);
375 if( http_hdr->protocol==NULL ) {
376 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
377 return -1;
379 strncpy( http_hdr->protocol, http_hdr->buffer, len );
380 http_hdr->protocol[len]='\0';
381 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
382 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
383 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
384 return -1;
388 // Get the status code
389 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
390 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
391 return -1;
393 hdr_ptr += 4;
395 // Get the reason phrase
396 ptr = strstr( hdr_ptr, "\n" );
397 if( hdr_ptr==NULL ) {
398 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
399 return -1;
401 len = ptr-hdr_ptr;
402 http_hdr->reason_phrase = malloc(len+1);
403 if( http_hdr->reason_phrase==NULL ) {
404 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
405 return -1;
407 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
408 if( http_hdr->reason_phrase[len-1]=='\r' ) {
409 len--;
411 http_hdr->reason_phrase[len]='\0';
413 // Set the position of the header separator: \r\n\r\n
414 hdr_sep_len = 4;
415 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
416 if( ptr==NULL ) {
417 ptr = strstr( http_hdr->buffer, "\n\n" );
418 if( ptr==NULL ) {
419 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
420 return -1;
422 hdr_sep_len = 2;
424 pos_hdr_sep = ptr-http_hdr->buffer;
426 // Point to the first line after the method line.
427 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
428 do {
429 ptr = hdr_ptr;
430 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
431 len = ptr-hdr_ptr;
432 if( len==0 ) break;
433 field = realloc(field, len+1);
434 if( field==NULL ) {
435 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
436 return -1;
438 strncpy( field, hdr_ptr, len );
439 field[len]='\0';
440 http_set_field( http_hdr, field );
441 hdr_ptr = ptr+((*ptr=='\r')?2:1);
442 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
444 free(field);
446 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
447 // Response has data!
448 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
449 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
452 http_hdr->is_parsed = 1;
453 return 0;
456 char *
457 http_build_request( HTTP_header_t *http_hdr ) {
458 char *ptr, *uri=NULL;
459 int len;
460 HTTP_field_t *field;
461 if( http_hdr==NULL ) return NULL;
463 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
464 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
465 else {
466 uri = malloc(strlen(http_hdr->uri) + 1);
467 if( uri==NULL ) {
468 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
469 return NULL;
471 strcpy(uri,http_hdr->uri);
474 //**** Compute the request length
475 // Add the Method line
476 len = strlen(http_hdr->method)+strlen(uri)+12;
477 // Add the fields
478 field = http_hdr->first_field;
479 while( field!=NULL ) {
480 len += strlen(field->field_name)+2;
481 field = field->next;
483 // Add the CRLF
484 len += 2;
485 // Add the body
486 if( http_hdr->body!=NULL ) {
487 len += http_hdr->body_size;
489 // Free the buffer if it was previously used
490 if( http_hdr->buffer!=NULL ) {
491 free( http_hdr->buffer );
492 http_hdr->buffer = NULL;
494 http_hdr->buffer = malloc(len+1);
495 if( http_hdr->buffer==NULL ) {
496 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
497 return NULL;
499 http_hdr->buffer_size = len;
501 //*** Building the request
502 ptr = http_hdr->buffer;
503 // Add the method line
504 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
505 field = http_hdr->first_field;
506 // Add the field
507 while( field!=NULL ) {
508 ptr += sprintf( ptr, "%s\r\n", field->field_name );
509 field = field->next;
511 ptr += sprintf( ptr, "\r\n" );
512 // Add the body
513 if( http_hdr->body!=NULL ) {
514 memcpy( ptr, http_hdr->body, http_hdr->body_size );
517 free(uri);
518 return http_hdr->buffer;
521 char *
522 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
523 if( http_hdr==NULL || field_name==NULL ) return NULL;
524 http_hdr->field_search_pos = http_hdr->first_field;
525 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 );
526 if( http_hdr->field_search==NULL ) {
527 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
528 return NULL;
530 strcpy( http_hdr->field_search, field_name );
531 return http_get_next_field( http_hdr );
534 char *
535 http_get_next_field( HTTP_header_t *http_hdr ) {
536 char *ptr;
537 HTTP_field_t *field;
538 if( http_hdr==NULL ) return NULL;
540 field = http_hdr->field_search_pos;
541 while( field!=NULL ) {
542 ptr = strstr( field->field_name, ":" );
543 if( ptr==NULL ) return NULL;
544 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
545 ptr++; // Skip the column
546 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
547 http_hdr->field_search_pos = field->next;
548 return ptr; // return the value without the field name
550 field = field->next;
552 return NULL;
555 void
556 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
557 HTTP_field_t *new_field;
558 if( http_hdr==NULL || field_name==NULL ) return;
560 new_field = malloc(sizeof(HTTP_field_t));
561 if( new_field==NULL ) {
562 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
563 return;
565 new_field->next = NULL;
566 new_field->field_name = malloc(strlen(field_name)+1);
567 if( new_field->field_name==NULL ) {
568 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
569 free(new_field);
570 return;
572 strcpy( new_field->field_name, field_name );
574 if( http_hdr->last_field==NULL ) {
575 http_hdr->first_field = new_field;
576 } else {
577 http_hdr->last_field->next = new_field;
579 http_hdr->last_field = new_field;
580 http_hdr->field_nb++;
583 void
584 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
585 if( http_hdr==NULL || method==NULL ) return;
587 http_hdr->method = malloc(strlen(method)+1);
588 if( http_hdr->method==NULL ) {
589 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
590 return;
592 strcpy( http_hdr->method, method );
595 void
596 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
597 if( http_hdr==NULL || uri==NULL ) return;
599 http_hdr->uri = malloc(strlen(uri)+1);
600 if( http_hdr->uri==NULL ) {
601 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
602 return;
604 strcpy( http_hdr->uri, uri );
607 static int
608 http_add_authentication( HTTP_header_t *http_hdr, const char *username, const char *password, const char *auth_str ) {
609 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
610 int encoded_len, pass_len=0, out_len;
611 size_t auth_len;
612 int res = -1;
613 if( http_hdr==NULL || username==NULL ) return -1;
615 if( password!=NULL ) {
616 pass_len = strlen(password);
619 usr_pass = malloc(strlen(username)+pass_len+2);
620 if( usr_pass==NULL ) {
621 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
622 goto out;
625 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
627 // Base 64 encode with at least 33% more data than the original size
628 encoded_len = strlen(usr_pass)*2;
629 b64_usr_pass = malloc(encoded_len);
630 if( b64_usr_pass==NULL ) {
631 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
632 goto out;
635 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
636 if( out_len<0 ) {
637 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
638 goto out;
641 b64_usr_pass[out_len]='\0';
643 auth_len = encoded_len + 100;
644 auth = malloc(auth_len);
645 if( auth==NULL ) {
646 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
647 goto out;
650 snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass);
651 http_set_field( http_hdr, auth );
652 res = 0;
654 out:
655 free( usr_pass );
656 free( b64_usr_pass );
657 free( auth );
659 return res;
663 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
664 return http_add_authentication(http_hdr, username, password, "Authorization");
668 http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
669 return http_add_authentication(http_hdr, username, password, "Proxy-Authorization");
672 void
673 http_debug_hdr( HTTP_header_t *http_hdr ) {
674 HTTP_field_t *field;
675 int i = 0;
676 if( http_hdr==NULL ) return;
678 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
679 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
680 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
681 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
682 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
683 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
684 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
685 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%zd]\n", http_hdr->body_size );
687 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
688 field = http_hdr->first_field;
689 while( field!=NULL ) {
690 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
691 field = field->next;
693 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
697 base64_encode(const void *enc, int encLen, char *out, int outMax) {
698 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
700 unsigned char *encBuf;
701 int outLen;
702 unsigned int bits;
703 unsigned int shift;
705 encBuf = (unsigned char*)enc;
706 outLen = 0;
707 bits = 0;
708 shift = 0;
709 outMax &= ~3;
711 while(1) {
712 if( encLen>0 ) {
713 // Shift in byte
714 bits <<= 8;
715 bits |= *encBuf;
716 shift += 8;
717 // Next byte
718 encBuf++;
719 encLen--;
720 } else if( shift>0 ) {
721 // Pad last bits to 6 bits - will end next loop
722 bits <<= 6 - shift;
723 shift = 6;
724 } else {
725 // As per RFC 2045, section 6.8,
726 // pad output as necessary: 0 to 2 '=' chars.
727 while( outLen & 3 ){
728 *out++ = '=';
729 outLen++;
732 return outLen;
735 // Encode 6 bit segments
736 while( shift>=6 ) {
737 if (outLen >= outMax)
738 return -1;
739 shift -= 6;
740 *out = b64[ (bits >> shift) & 0x3F ];
741 out++;
742 outLen++;
747 static void print_icy_metadata(HTTP_header_t *http_hdr) {
748 const char *field_data;
749 // note: I skip icy-notice1 and 2, as they contain html <BR>
750 // and are IMHO useless info ::atmos
751 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
752 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
753 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
754 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
755 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
756 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
757 // XXX: does this really mean public server? ::atmos
758 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
759 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
760 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
761 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
764 //! If this function succeeds you must closesocket stream->fd
765 static int http_streaming_start(stream_t *stream, int* file_format) {
766 HTTP_header_t *http_hdr = NULL;
767 int fd = stream->fd;
768 int res = STREAM_UNSUPPORTED;
769 int redirect = 0;
770 int auth_retry=0;
771 int seekable=0;
772 char *content_type;
773 const char *content_length;
774 char *next_url;
775 URL_t *url = stream->streaming_ctrl->url;
779 redirect = 0;
780 if (fd > 0) closesocket(fd);
781 fd = http_send_request( url, 0 );
782 if( fd<0 ) {
783 goto err_out;
786 http_free(http_hdr);
787 http_hdr = http_read_response( fd );
788 if( http_hdr==NULL ) {
789 goto err_out;
792 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
793 http_debug_hdr( http_hdr );
796 // Check if we can make partial content requests and thus seek in http-streams
797 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
798 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
799 const char *server = http_get_field(http_hdr, "Server");
800 if (accept_ranges)
801 seekable = strncmp(accept_ranges,"bytes",5)==0;
802 else if (server && strcmp(server, "gvs 1.0") == 0)
803 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
806 print_icy_metadata(http_hdr);
808 // Check if the response is an ICY status_code reason_phrase
809 if( !strcasecmp(http_hdr->protocol, "ICY") ||
810 http_get_field(http_hdr, "Icy-MetaInt") ) {
811 switch( http_hdr->status_code ) {
812 case 200: { // OK
813 char *field_data;
814 // If content-type == video/nsv we most likely have a winamp video stream
815 // otherwise it should be mp3. if there are more types consider adding mime type
816 // handling like later
817 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
818 *file_format = DEMUXER_TYPE_NSV;
819 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
820 *file_format = DEMUXER_TYPE_AAC;
821 else
822 *file_format = DEMUXER_TYPE_AUDIO;
823 res = STREAM_ERROR;
824 goto out;
826 case 400: // Server Full
827 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
828 goto err_out;
829 case 401: // Service Unavailable
830 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
831 goto err_out;
832 case 403: // Service Forbidden
833 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
834 goto err_out;
835 case 404: // Resource Not Found
836 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
837 goto err_out;
838 default:
839 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
840 goto err_out;
844 // Assume standard http if not ICY
845 switch( http_hdr->status_code ) {
846 case 200: // OK
847 content_length = http_get_field(http_hdr, "Content-Length");
848 if (content_length) {
849 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
850 stream->end_pos = atoll(content_length);
852 // Look if we can use the Content-Type
853 content_type = http_get_field( http_hdr, "Content-Type" );
854 if( content_type!=NULL ) {
855 unsigned int i;
857 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
858 // Check in the mime type table for a demuxer type
859 for (i = 0; mime_type_table[i].mime_type != NULL; i++) {
860 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
861 *file_format = mime_type_table[i].demuxer_type;
862 res = seekable;
863 goto out;
867 // Not found in the mime type table, don't fail,
868 // we should try raw HTTP
869 res = seekable;
870 goto out;
871 // Redirect
872 case 301: // Permanently
873 case 302: // Temporarily
874 case 303: // See Other
875 // TODO: RFC 2616, recommand to detect infinite redirection loops
876 next_url = http_get_field( http_hdr, "Location" );
877 if( next_url!=NULL ) {
878 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
879 stream->streaming_ctrl->url = url_redirect( &url, next_url );
880 if (!strcasecmp(url->protocol, "mms")) {
881 res = STREAM_REDIRECTED;
882 goto err_out;
884 if (strcasecmp(url->protocol, "http")) {
885 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
886 goto err_out;
888 if (is_ultravox) {
889 free(url->protocol);
890 url->protocol = strdup("unsv");
892 redirect = 1;
894 break;
895 case 401: // Authentication required
896 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
897 goto err_out;
898 redirect = 1;
899 break;
900 default:
901 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
902 goto err_out;
904 } while( redirect );
906 err_out:
907 if (fd > 0) closesocket( fd );
908 fd = -1;
909 http_free( http_hdr );
910 http_hdr = NULL;
911 out:
912 stream->streaming_ctrl->data = (void*)http_hdr;
913 stream->fd = fd;
914 return res;
917 static int fixup_open(stream_t *stream,int seekable) {
918 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
919 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
920 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
922 stream->type = STREAMTYPE_STREAM;
923 if(!is_icy && !is_ultravox && seekable)
925 stream->flags |= MP_STREAM_SEEK;
926 stream->seek = http_seek;
928 stream->streaming_ctrl->bandwidth = network_bandwidth;
929 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
930 if(nop_streaming_start( stream )) {
931 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
932 if (stream->fd >= 0)
933 closesocket(stream->fd);
934 stream->fd = -1;
935 streaming_ctrl_free(stream->streaming_ctrl);
936 stream->streaming_ctrl = NULL;
937 return STREAM_UNSUPPORTED;
940 fixup_network_stream_cache(stream);
941 return STREAM_OK;
944 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
945 int seekable=0;
946 URL_t *url;
948 stream->streaming_ctrl = streaming_ctrl_new();
949 if( stream->streaming_ctrl==NULL ) {
950 return STREAM_ERROR;
952 stream->streaming_ctrl->bandwidth = network_bandwidth;
953 url = url_new(stream->url);
954 stream->streaming_ctrl->url = check4proxies(url);
955 url_free(url);
957 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
958 seekable = http_streaming_start(stream, file_format);
959 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
960 if (stream->fd >= 0)
961 closesocket(stream->fd);
962 stream->fd = -1;
963 if (seekable == STREAM_REDIRECTED)
964 return seekable;
965 streaming_ctrl_free(stream->streaming_ctrl);
966 stream->streaming_ctrl = NULL;
967 return STREAM_UNSUPPORTED;
970 return fixup_open(stream, seekable);
973 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
974 int seekable=0;
975 URL_t *url;
977 stream->streaming_ctrl = streaming_ctrl_new();
978 if( stream->streaming_ctrl==NULL ) {
979 return STREAM_ERROR;
981 stream->streaming_ctrl->bandwidth = network_bandwidth;
982 url = url_new(stream->url);
983 stream->streaming_ctrl->url = check4proxies(url);
984 url_free(url);
986 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
987 seekable = http_streaming_start(stream, file_format);
988 if(seekable < 0) {
989 if (stream->fd >= 0)
990 closesocket(stream->fd);
991 stream->fd = -1;
992 streaming_ctrl_free(stream->streaming_ctrl);
993 stream->streaming_ctrl = NULL;
994 return STREAM_UNSUPPORTED;
997 return fixup_open(stream, seekable);
1001 const stream_info_t stream_info_http1 = {
1002 "http streaming",
1003 "null",
1004 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
1005 "plain http",
1006 open_s1,
1007 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
1008 NULL,
1009 0 // Urls are an option string
1012 const stream_info_t stream_info_http2 = {
1013 "http streaming",
1014 "null",
1015 "Bertrand, Albeu, Arpi? who?",
1016 "plain http, also used as fallback for many other protocols",
1017 open_s2,
1018 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
1019 NULL,
1020 0 // Urls are an option string