vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / stream / http.c
blob34ef49e68161d65eb7d2ba18fdf7d17686d45e45
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"
44 #include "libavutil/base64.h"
46 extern int stream_cache_size;
47 extern int network_bandwidth;
49 typedef struct {
50 unsigned metaint;
51 unsigned metapos;
52 int is_ultravox;
53 } scast_data_t;
55 /**
56 * \brief first read any data from sc->buffer then from fd
57 * \param fd file descriptor to read data from
58 * \param buffer buffer to read into
59 * \param len how many bytes to read
60 * \param sc streaming control containing buffer to read from first
61 * \return len unless there is a read error or eof
63 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) {
64 unsigned pos = 0;
65 unsigned cp_len = sc->buffer_size - sc->buffer_pos;
66 if (cp_len > len)
67 cp_len = len;
68 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len);
69 sc->buffer_pos += cp_len;
70 pos += cp_len;
71 while (pos < len) {
72 int ret = recv(fd, &buffer[pos], len - pos, 0);
73 if (ret <= 0)
74 break;
75 pos += ret;
77 return pos;
80 /**
81 * \brief read and process (i.e. discard *g*) a block of ultravox metadata
82 * \param fd file descriptor to read from
83 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
84 * \return number of real data before next metadata block starts or 0 on error
86 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) {
87 unsigned metaint;
88 unsigned char info[6] = {0, 0, 0, 0, 0, 0};
89 int info_read;
90 do {
91 info_read = my_read(fd, info, 1, sc);
92 if (info[0] == 0x00)
93 info_read = my_read(fd, info, 6, sc);
94 else
95 info_read += my_read(fd, &info[1], 5, sc);
96 if (info_read != 6) // read error or eof
97 return 0;
98 // sync byte and reserved flags
99 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) {
100 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n");
101 return 0;
103 if (info[1] & 0x01)
104 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n");
105 metaint = info[4] << 8 | info[5];
106 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense
107 char *metabuf = malloc(metaint);
108 my_read(fd, metabuf, metaint, sc);
109 free(metabuf);
111 } while ((info[3] & 0xf) < 0x07);
112 return metaint;
116 * \brief read one scast meta data entry and print it
117 * \param fd file descriptor to read from
118 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
120 static void scast_meta_read(int fd, streaming_ctrl_t *sc) {
121 unsigned char tmp = 0;
122 unsigned metalen;
123 my_read(fd, &tmp, 1, sc);
124 metalen = tmp * 16;
125 if (metalen > 0) {
126 int i;
127 uint8_t *info = malloc(metalen + 1);
128 unsigned nlen = my_read(fd, info, metalen, sc);
129 // avoid breaking the user's terminal too much
130 if (nlen > 256) nlen = 256;
131 for (i = 0; i < nlen; i++)
132 if (info[i] && info[i] < 32) info[i] = '?';
133 info[nlen] = 0;
134 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info);
135 free(info);
140 * \brief read data from scast/ultravox stream without any metadata
141 * \param fd file descriptor to read from
142 * \param buffer buffer to read data into
143 * \param size number of bytes to read
144 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd
146 static int scast_streaming_read(int fd, char *buffer, int size,
147 streaming_ctrl_t *sc) {
148 scast_data_t *sd = (scast_data_t *)sc->data;
149 unsigned block, ret;
150 unsigned done = 0;
152 // first read remaining data up to next metadata
153 block = sd->metaint - sd->metapos;
154 if (block > size)
155 block = size;
156 ret = my_read(fd, buffer, block, sc);
157 sd->metapos += ret;
158 done += ret;
159 if (ret != block) // read problems or eof
160 size = done;
162 while (done < size) { // now comes the metadata
163 if (sd->is_ultravox)
165 sd->metaint = uvox_meta_read(fd, sc);
166 if (!sd->metaint)
167 size = done;
169 else
170 scast_meta_read(fd, sc); // read and display metadata
171 sd->metapos = 0;
172 block = size - done;
173 if (block > sd->metaint)
174 block = sd->metaint;
175 ret = my_read(fd, &buffer[done], block, sc);
176 sd->metapos += ret;
177 done += ret;
178 if (ret != block) // read problems or eof
179 size = done;
181 return done;
184 static int scast_streaming_start(stream_t *stream) {
185 int metaint;
186 scast_data_t *scast_data;
187 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
188 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
189 if (!stream || stream->fd < 0 || !http_hdr)
190 return -1;
191 if (is_ultravox)
192 metaint = 0;
193 else {
194 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt"));
195 if (metaint <= 0)
196 return -1;
198 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
199 stream->streaming_ctrl->buffer_size = http_hdr->body_size;
200 stream->streaming_ctrl->buffer_pos = 0;
201 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
202 scast_data = malloc(sizeof(scast_data_t));
203 scast_data->metaint = metaint;
204 scast_data->metapos = 0;
205 scast_data->is_ultravox = is_ultravox;
206 http_free(http_hdr);
207 stream->streaming_ctrl->data = scast_data;
208 stream->streaming_ctrl->streaming_read = scast_streaming_read;
209 stream->streaming_ctrl->streaming_seek = NULL;
210 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes
211 stream->streaming_ctrl->buffering = 1;
212 stream->streaming_ctrl->status = streaming_playing_e;
213 return 0;
216 static int nop_streaming_start( stream_t *stream ) {
217 HTTP_header_t *http_hdr = NULL;
218 char *next_url=NULL;
219 URL_t *rd_url=NULL;
220 int fd,ret;
221 if( stream==NULL ) return -1;
223 fd = stream->fd;
224 if( fd<0 ) {
225 fd = http_send_request( stream->streaming_ctrl->url, 0 );
226 if( fd<0 ) return -1;
227 http_hdr = http_read_response( fd );
228 if( http_hdr==NULL ) return -1;
230 switch( http_hdr->status_code ) {
231 case 200: // OK
232 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
233 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
234 if( http_hdr->body_size>0 ) {
235 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
236 http_free( http_hdr );
237 return -1;
240 break;
241 // Redirect
242 case 301: // Permanently
243 case 302: // Temporarily
244 case 303: // See Other
245 ret=-1;
246 next_url = http_get_field( http_hdr, "Location" );
248 if (next_url != NULL)
249 rd_url=url_new(next_url);
251 if (next_url != NULL && rd_url != NULL) {
252 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url);
253 stream->streaming_ctrl->url=check4proxies(rd_url);
254 ret=nop_streaming_start(stream); //recursively get streaming started
255 } else {
256 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n");
257 closesocket( fd );
258 fd = -1;
260 return ret;
261 break;
262 case 401: //Authorization required
263 case 403: //Forbidden
264 case 404: //Not found
265 case 500: //Server Error
266 default:
267 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
268 closesocket( fd );
269 fd = -1;
270 return -1;
271 break;
273 stream->fd = fd;
274 } else {
275 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data;
276 if( http_hdr->body_size>0 ) {
277 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
278 http_free( http_hdr );
279 stream->streaming_ctrl->data = NULL;
280 return -1;
285 if( http_hdr ) {
286 http_free( http_hdr );
287 stream->streaming_ctrl->data = NULL;
290 stream->streaming_ctrl->streaming_read = nop_streaming_read;
291 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
292 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes
293 stream->streaming_ctrl->buffering = 1;
294 stream->streaming_ctrl->status = streaming_playing_e;
295 return 0;
298 HTTP_header_t *
299 http_new_header(void) {
300 HTTP_header_t *http_hdr;
302 http_hdr = malloc(sizeof(HTTP_header_t));
303 if( http_hdr==NULL ) return NULL;
304 memset( http_hdr, 0, sizeof(HTTP_header_t) );
306 return http_hdr;
309 void
310 http_free( HTTP_header_t *http_hdr ) {
311 HTTP_field_t *field, *field2free;
312 if( http_hdr==NULL ) return;
313 free(http_hdr->protocol);
314 free(http_hdr->uri);
315 free(http_hdr->reason_phrase);
316 free(http_hdr->field_search);
317 free(http_hdr->method);
318 free(http_hdr->buffer);
319 field = http_hdr->first_field;
320 while( field!=NULL ) {
321 field2free = field;
322 free(field->field_name);
323 field = field->next;
324 free( field2free );
326 free( http_hdr );
327 http_hdr = NULL;
331 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
332 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
334 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) {
335 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n");
336 return -1;
338 http_hdr->buffer = realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
339 if( http_hdr->buffer==NULL ) {
340 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
341 return -1;
343 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
344 http_hdr->buffer_size += length;
345 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
346 return http_hdr->buffer_size;
350 http_is_header_entire( HTTP_header_t *http_hdr ) {
351 if( http_hdr==NULL ) return -1;
352 if( http_hdr->buffer==NULL ) return 0; // empty
354 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
355 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
356 return 1;
360 http_response_parse( HTTP_header_t *http_hdr ) {
361 char *hdr_ptr, *ptr;
362 char *field=NULL;
363 int pos_hdr_sep, hdr_sep_len;
364 size_t len;
365 if( http_hdr==NULL ) return -1;
366 if( http_hdr->is_parsed ) return 0;
368 // Get the protocol
369 hdr_ptr = strstr( http_hdr->buffer, " " );
370 if( hdr_ptr==NULL ) {
371 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
372 return -1;
374 len = hdr_ptr-http_hdr->buffer;
375 http_hdr->protocol = malloc(len+1);
376 if( http_hdr->protocol==NULL ) {
377 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
378 return -1;
380 strncpy( http_hdr->protocol, http_hdr->buffer, len );
381 http_hdr->protocol[len]='\0';
382 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
383 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
384 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
385 return -1;
389 // Get the status code
390 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
391 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
392 return -1;
394 hdr_ptr += 4;
396 // Get the reason phrase
397 ptr = strstr( hdr_ptr, "\n" );
398 if( hdr_ptr==NULL ) {
399 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
400 return -1;
402 len = ptr-hdr_ptr;
403 http_hdr->reason_phrase = malloc(len+1);
404 if( http_hdr->reason_phrase==NULL ) {
405 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
406 return -1;
408 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
409 if( http_hdr->reason_phrase[len-1]=='\r' ) {
410 len--;
412 http_hdr->reason_phrase[len]='\0';
414 // Set the position of the header separator: \r\n\r\n
415 hdr_sep_len = 4;
416 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
417 if( ptr==NULL ) {
418 ptr = strstr( http_hdr->buffer, "\n\n" );
419 if( ptr==NULL ) {
420 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
421 return -1;
423 hdr_sep_len = 2;
425 pos_hdr_sep = ptr-http_hdr->buffer;
427 // Point to the first line after the method line.
428 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
429 do {
430 ptr = hdr_ptr;
431 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
432 len = ptr-hdr_ptr;
433 if( len==0 ) break;
434 field = realloc(field, len+1);
435 if( field==NULL ) {
436 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
437 return -1;
439 strncpy( field, hdr_ptr, len );
440 field[len]='\0';
441 http_set_field( http_hdr, field );
442 hdr_ptr = ptr+((*ptr=='\r')?2:1);
443 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
445 free(field);
447 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
448 // Response has data!
449 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
450 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
453 http_hdr->is_parsed = 1;
454 return 0;
457 char *
458 http_build_request( HTTP_header_t *http_hdr ) {
459 char *ptr, *uri=NULL;
460 int len;
461 HTTP_field_t *field;
462 if( http_hdr==NULL ) return NULL;
464 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
465 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
466 else {
467 uri = malloc(strlen(http_hdr->uri) + 1);
468 if( uri==NULL ) {
469 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
470 return NULL;
472 strcpy(uri,http_hdr->uri);
475 //**** Compute the request length
476 // Add the Method line
477 len = strlen(http_hdr->method)+strlen(uri)+12;
478 // Add the fields
479 field = http_hdr->first_field;
480 while( field!=NULL ) {
481 len += strlen(field->field_name)+2;
482 field = field->next;
484 // Add the CRLF
485 len += 2;
486 // Add the body
487 if( http_hdr->body!=NULL ) {
488 len += http_hdr->body_size;
490 // Free the buffer if it was previously used
491 if( http_hdr->buffer!=NULL ) {
492 free( http_hdr->buffer );
493 http_hdr->buffer = NULL;
495 http_hdr->buffer = malloc(len+1);
496 if( http_hdr->buffer==NULL ) {
497 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
498 return NULL;
500 http_hdr->buffer_size = len;
502 //*** Building the request
503 ptr = http_hdr->buffer;
504 // Add the method line
505 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
506 field = http_hdr->first_field;
507 // Add the field
508 while( field!=NULL ) {
509 ptr += sprintf( ptr, "%s\r\n", field->field_name );
510 field = field->next;
512 ptr += sprintf( ptr, "\r\n" );
513 // Add the body
514 if( http_hdr->body!=NULL ) {
515 memcpy( ptr, http_hdr->body, http_hdr->body_size );
518 free(uri);
519 return http_hdr->buffer;
522 char *
523 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
524 if( http_hdr==NULL || field_name==NULL ) return NULL;
525 http_hdr->field_search_pos = http_hdr->first_field;
526 http_hdr->field_search = realloc( http_hdr->field_search, strlen(field_name)+1 );
527 if( http_hdr->field_search==NULL ) {
528 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
529 return NULL;
531 strcpy( http_hdr->field_search, field_name );
532 return http_get_next_field( http_hdr );
535 char *
536 http_get_next_field( HTTP_header_t *http_hdr ) {
537 char *ptr;
538 HTTP_field_t *field;
539 if( http_hdr==NULL ) return NULL;
541 field = http_hdr->field_search_pos;
542 while( field!=NULL ) {
543 ptr = strstr( field->field_name, ":" );
544 if( ptr==NULL ) return NULL;
545 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
546 ptr++; // Skip the column
547 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
548 http_hdr->field_search_pos = field->next;
549 return ptr; // return the value without the field name
551 field = field->next;
553 return NULL;
556 void
557 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
558 HTTP_field_t *new_field;
559 if( http_hdr==NULL || field_name==NULL ) return;
561 new_field = malloc(sizeof(HTTP_field_t));
562 if( new_field==NULL ) {
563 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
564 return;
566 new_field->next = NULL;
567 new_field->field_name = malloc(strlen(field_name)+1);
568 if( new_field->field_name==NULL ) {
569 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
570 free(new_field);
571 return;
573 strcpy( new_field->field_name, field_name );
575 if( http_hdr->last_field==NULL ) {
576 http_hdr->first_field = new_field;
577 } else {
578 http_hdr->last_field->next = new_field;
580 http_hdr->last_field = new_field;
581 http_hdr->field_nb++;
584 void
585 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
586 if( http_hdr==NULL || method==NULL ) return;
588 http_hdr->method = malloc(strlen(method)+1);
589 if( http_hdr->method==NULL ) {
590 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
591 return;
593 strcpy( http_hdr->method, method );
596 void
597 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
598 if( http_hdr==NULL || uri==NULL ) return;
600 http_hdr->uri = malloc(strlen(uri)+1);
601 if( http_hdr->uri==NULL ) {
602 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
603 return;
605 strcpy( http_hdr->uri, uri );
608 static int
609 http_add_authentication( HTTP_header_t *http_hdr, const char *username, const char *password, const char *auth_str ) {
610 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL;
611 int encoded_len, pass_len=0;
612 size_t auth_len, usr_pass_len;
613 int res = -1;
614 if( http_hdr==NULL || username==NULL ) return -1;
616 if( password!=NULL ) {
617 pass_len = strlen(password);
620 usr_pass_len = strlen(username) + 1 + pass_len;
621 usr_pass = malloc(usr_pass_len + 1);
622 if( usr_pass==NULL ) {
623 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
624 goto out;
627 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
629 encoded_len = AV_BASE64_SIZE(usr_pass_len);
630 b64_usr_pass = malloc(encoded_len);
631 if( b64_usr_pass==NULL ) {
632 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
633 goto out;
635 av_base64_encode(b64_usr_pass, encoded_len, usr_pass, usr_pass_len);
637 auth_len = encoded_len + 100;
638 auth = malloc(auth_len);
639 if( auth==NULL ) {
640 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
641 goto out;
644 snprintf(auth, auth_len, "%s: Basic %s", auth_str, b64_usr_pass);
645 http_set_field( http_hdr, auth );
646 res = 0;
648 out:
649 free( usr_pass );
650 free( b64_usr_pass );
651 free( auth );
653 return res;
657 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
658 return http_add_authentication(http_hdr, username, password, "Authorization");
662 http_add_basic_proxy_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
663 return http_add_authentication(http_hdr, username, password, "Proxy-Authorization");
666 void
667 http_debug_hdr( HTTP_header_t *http_hdr ) {
668 HTTP_field_t *field;
669 int i = 0;
670 if( http_hdr==NULL ) return;
672 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
673 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
674 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
675 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
676 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
677 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
678 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
679 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%zd]\n", http_hdr->body_size );
681 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
682 field = http_hdr->first_field;
683 while( field!=NULL ) {
684 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
685 field = field->next;
687 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
690 static void print_icy_metadata(HTTP_header_t *http_hdr) {
691 const char *field_data;
692 // note: I skip icy-notice1 and 2, as they contain html <BR>
693 // and are IMHO useless info ::atmos
694 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL )
695 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data);
696 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL )
697 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data);
698 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL )
699 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data);
700 // XXX: does this really mean public server? ::atmos
701 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL )
702 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no");
703 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL )
704 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data);
707 //! If this function succeeds you must closesocket stream->fd
708 static int http_streaming_start(stream_t *stream, int* file_format) {
709 HTTP_header_t *http_hdr = NULL;
710 int fd = stream->fd;
711 int res = STREAM_UNSUPPORTED;
712 int redirect = 0;
713 int auth_retry=0;
714 int seekable=0;
715 char *content_type;
716 const char *content_length;
717 char *next_url;
718 URL_t *url = stream->streaming_ctrl->url;
722 redirect = 0;
723 if (fd > 0) closesocket(fd);
724 fd = http_send_request( url, 0 );
725 if( fd<0 ) {
726 goto err_out;
729 http_free(http_hdr);
730 http_hdr = http_read_response( fd );
731 if( http_hdr==NULL ) {
732 goto err_out;
735 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
736 http_debug_hdr( http_hdr );
739 // Check if we can make partial content requests and thus seek in http-streams
740 if( http_hdr!=NULL && http_hdr->status_code==200 ) {
741 const char *accept_ranges = http_get_field(http_hdr,"Accept-Ranges");
742 const char *server = http_get_field(http_hdr, "Server");
743 if (accept_ranges)
744 seekable = strncmp(accept_ranges,"bytes",5)==0;
745 else if (server && strcmp(server, "gvs 1.0") == 0)
746 seekable = 1; // HACK for youtube incorrectly claiming not to support seeking
749 print_icy_metadata(http_hdr);
751 // Check if the response is an ICY status_code reason_phrase
752 if( !strcasecmp(http_hdr->protocol, "ICY") ||
753 http_get_field(http_hdr, "Icy-MetaInt") ) {
754 switch( http_hdr->status_code ) {
755 case 200: { // OK
756 char *field_data;
757 // If content-type == video/nsv we most likely have a winamp video stream
758 // otherwise it should be mp3. if there are more types consider adding mime type
759 // handling like later
760 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox")))
761 *file_format = DEMUXER_TYPE_NSV;
762 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac")))
763 *file_format = DEMUXER_TYPE_AAC;
764 else
765 *file_format = DEMUXER_TYPE_AUDIO;
766 res = STREAM_ERROR;
767 goto out;
769 case 400: // Server Full
770 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n");
771 goto err_out;
772 case 401: // Service Unavailable
773 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n");
774 goto err_out;
775 case 403: // Service Forbidden
776 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n");
777 goto err_out;
778 case 404: // Resource Not Found
779 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n");
780 goto err_out;
781 default:
782 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n");
783 goto err_out;
787 // Assume standard http if not ICY
788 switch( http_hdr->status_code ) {
789 case 200: // OK
790 content_length = http_get_field(http_hdr, "Content-Length");
791 if (content_length) {
792 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", content_length);
793 stream->end_pos = atoll(content_length);
795 // Look if we can use the Content-Type
796 content_type = http_get_field( http_hdr, "Content-Type" );
797 if( content_type!=NULL ) {
798 unsigned int i;
800 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type );
801 // Check in the mime type table for a demuxer type
802 for (i = 0; mime_type_table[i].mime_type != NULL; i++) {
803 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) {
804 *file_format = mime_type_table[i].demuxer_type;
805 res = seekable;
806 goto out;
810 // Not found in the mime type table, don't fail,
811 // we should try raw HTTP
812 res = seekable;
813 goto out;
814 // Redirect
815 case 301: // Permanently
816 case 302: // Temporarily
817 case 303: // See Other
818 // TODO: RFC 2616, recommand to detect infinite redirection loops
819 next_url = http_get_field( http_hdr, "Location" );
820 if( next_url!=NULL ) {
821 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
822 stream->streaming_ctrl->url = url_redirect( &url, next_url );
823 if (!strcasecmp(url->protocol, "mms")) {
824 res = STREAM_REDIRECTED;
825 goto err_out;
827 if (strcasecmp(url->protocol, "http")) {
828 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol);
829 goto err_out;
831 if (is_ultravox) {
832 free(url->protocol);
833 url->protocol = strdup("unsv");
835 redirect = 1;
837 break;
838 case 401: // Authentication required
839 if( http_authenticate(http_hdr, url, &auth_retry)<0 )
840 goto err_out;
841 redirect = 1;
842 break;
843 default:
844 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
845 goto err_out;
847 } while( redirect );
849 err_out:
850 if (fd > 0) closesocket( fd );
851 fd = -1;
852 http_free( http_hdr );
853 http_hdr = NULL;
854 out:
855 stream->streaming_ctrl->data = (void*)http_hdr;
856 stream->fd = fd;
857 return res;
860 static int fixup_open(stream_t *stream,int seekable) {
861 HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
862 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt");
863 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
865 stream->type = STREAMTYPE_STREAM;
866 if(!is_icy && !is_ultravox && seekable)
868 stream->flags |= MP_STREAM_SEEK;
869 stream->seek = http_seek;
871 stream->streaming_ctrl->bandwidth = network_bandwidth;
872 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream))
873 if(nop_streaming_start( stream )) {
874 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n");
875 if (stream->fd >= 0)
876 closesocket(stream->fd);
877 stream->fd = -1;
878 streaming_ctrl_free(stream->streaming_ctrl);
879 stream->streaming_ctrl = NULL;
880 return STREAM_UNSUPPORTED;
883 fixup_network_stream_cache(stream);
884 return STREAM_OK;
887 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) {
888 int seekable=0;
889 URL_t *url;
891 stream->streaming_ctrl = streaming_ctrl_new();
892 if( stream->streaming_ctrl==NULL ) {
893 return STREAM_ERROR;
895 stream->streaming_ctrl->bandwidth = network_bandwidth;
896 url = url_new(stream->url);
897 stream->streaming_ctrl->url = check4proxies(url);
898 url_free(url);
900 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url);
901 seekable = http_streaming_start(stream, file_format);
902 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) {
903 if (stream->fd >= 0)
904 closesocket(stream->fd);
905 stream->fd = -1;
906 if (seekable == STREAM_REDIRECTED)
907 return seekable;
908 streaming_ctrl_free(stream->streaming_ctrl);
909 stream->streaming_ctrl = NULL;
910 return STREAM_UNSUPPORTED;
913 return fixup_open(stream, seekable);
916 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) {
917 int seekable=0;
918 URL_t *url;
920 stream->streaming_ctrl = streaming_ctrl_new();
921 if( stream->streaming_ctrl==NULL ) {
922 return STREAM_ERROR;
924 stream->streaming_ctrl->bandwidth = network_bandwidth;
925 url = url_new(stream->url);
926 stream->streaming_ctrl->url = check4proxies(url);
927 url_free(url);
929 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url);
930 seekable = http_streaming_start(stream, file_format);
931 if(seekable < 0) {
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 return fixup_open(stream, seekable);
944 const stream_info_t stream_info_http1 = {
945 "http streaming",
946 "null",
947 "Bertrand, Albeau, Reimar Doeffinger, Arpi?",
948 "plain http",
949 open_s1,
950 {"http", "http_proxy", "unsv", "icyx", "noicyx", NULL},
951 NULL,
952 0 // Urls are an option string
955 const stream_info_t stream_info_http2 = {
956 "http streaming",
957 "null",
958 "Bertrand, Albeu, Arpi? who?",
959 "plain http, also used as fallback for many other protocols",
960 open_s2,
961 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback
962 NULL,
963 0 // Urls are an option string