Mercurial > mplayer.hg
annotate stream/http.c @ 27464:be85aae103f2
Move duplicated '#define closesocket close' into network.h along with
network-related #include #ifdeffery.
| author | diego |
|---|---|
| date | Fri, 29 Aug 2008 22:55:39 +0000 |
| parents | 5a30f5bc23a0 |
| children | c0b233cd30ca |
| rev | line source |
|---|---|
| 902 | 1 /* |
| 2 * HTTP Helper | |
| 3 * by Bertrand Baudet <bertrand_baudet@yahoo.com> | |
| 4 * (C) 2001, MPlayer team. | |
| 5 */ | |
| 6 | |
|
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
7 #include "config.h" |
|
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
8 |
| 870 | 9 #include <stdio.h> |
| 10 #include <stdlib.h> | |
| 11 #include <string.h> | |
| 15585 | 12 #include <unistd.h> |
| 870 | 13 |
|
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
14 #include "url.h" |
| 5915 | 15 #include "mp_msg.h" |
| 870 | 16 |
| 15585 | 17 #include "stream.h" |
|
19312
ab8d6b6deb63
proper inclusion of demuxer.h (including libmpdemux in Makefile only was to make previous split easier)
ben
parents:
19271
diff
changeset
|
18 #include "libmpdemux/demuxer.h" |
| 15585 | 19 #include "network.h" |
| 20 #include "help_mp.h" | |
| 21 | |
| 22 | |
| 25246 | 23 extern const mime_struct_t mime_type_table[]; |
| 15585 | 24 extern int stream_cache_size; |
| 25 extern int network_bandwidth; | |
| 26 | |
| 27 extern int http_seek(stream_t *stream, off_t pos); | |
| 28 | |
| 16013 | 29 typedef struct { |
| 30 unsigned metaint; | |
| 31 unsigned metapos; | |
| 32 int is_ultravox; | |
| 33 } scast_data_t; | |
| 34 | |
| 35 /** | |
| 36 * \brief first read any data from sc->buffer then from fd | |
| 37 * \param fd file descriptor to read data from | |
| 38 * \param buffer buffer to read into | |
| 39 * \param len how many bytes to read | |
| 40 * \param sc streaming control containing buffer to read from first | |
| 41 * \return len unless there is a read error or eof | |
| 42 */ | |
| 43 static unsigned my_read(int fd, char *buffer, int len, streaming_ctrl_t *sc) { | |
| 44 unsigned pos = 0; | |
| 45 unsigned cp_len = sc->buffer_size - sc->buffer_pos; | |
| 46 if (cp_len > len) | |
| 47 cp_len = len; | |
| 48 memcpy(buffer, &sc->buffer[sc->buffer_pos], cp_len); | |
| 49 sc->buffer_pos += cp_len; | |
| 50 pos += cp_len; | |
| 51 while (pos < len) { | |
| 16070 | 52 int ret = recv(fd, &buffer[pos], len - pos, 0); |
| 16013 | 53 if (ret <= 0) |
| 54 break; | |
| 55 pos += ret; | |
| 56 } | |
| 57 return pos; | |
| 58 } | |
| 59 | |
| 16032 | 60 /** |
| 61 * \brief read and process (i.e. discard *g*) a block of ultravox metadata | |
| 62 * \param fd file descriptor to read from | |
| 63 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 64 * \return number of real data before next metadata block starts or 0 on error | |
| 65 */ | |
| 16013 | 66 static unsigned uvox_meta_read(int fd, streaming_ctrl_t *sc) { |
| 67 unsigned metaint; | |
| 16070 | 68 unsigned char info[6] = {0, 0, 0, 0, 0, 0}; |
| 69 int info_read; | |
| 16013 | 70 do { |
| 16070 | 71 info_read = my_read(fd, info, 1, sc); |
| 16013 | 72 if (info[0] == 0x00) |
| 16070 | 73 info_read = my_read(fd, info, 6, sc); |
| 16013 | 74 else |
| 16070 | 75 info_read += my_read(fd, &info[1], 5, sc); |
| 76 if (info_read != 6) // read error or eof | |
| 77 return 0; | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
78 // sync byte and reserved flags |
|
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
79 if (info[0] != 0x5a || (info[1] & 0xfc) != 0x00) { |
| 16013 | 80 mp_msg(MSGT_DEMUXER, MSGL_ERR, "Invalid or unknown uvox metadata\n"); |
| 81 return 0; | |
| 82 } | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
83 if (info[1] & 0x01) |
|
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
84 mp_msg(MSGT_DEMUXER, MSGL_WARN, "Encrypted ultravox data\n"); |
| 16013 | 85 metaint = info[4] << 8 | info[5]; |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
86 if ((info[3] & 0xf) < 0x07) { // discard any metadata nonsense |
| 16013 | 87 char *metabuf = malloc(metaint); |
| 88 my_read(fd, metabuf, metaint, sc); | |
| 89 free(metabuf); | |
| 90 } | |
|
16031
c2e78215f0d9
Ultravox improvements according to specs (didn't know they existed *g*)
reimar
parents:
16013
diff
changeset
|
91 } while ((info[3] & 0xf) < 0x07); |
| 16013 | 92 return metaint; |
| 93 } | |
| 94 | |
| 95 /** | |
| 96 * \brief read one scast meta data entry and print it | |
| 16032 | 97 * \param fd file descriptor to read from |
| 98 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 16013 | 99 */ |
| 100 static void scast_meta_read(int fd, streaming_ctrl_t *sc) { | |
| 101 unsigned char tmp = 0; | |
| 102 unsigned metalen; | |
| 103 my_read(fd, &tmp, 1, sc); | |
| 104 metalen = tmp * 16; | |
| 105 if (metalen > 0) { | |
| 18879 | 106 char *info = malloc(metalen + 1); |
| 16013 | 107 unsigned nlen = my_read(fd, info, metalen, sc); |
| 108 info[nlen] = 0; | |
| 109 mp_msg(MSGT_DEMUXER, MSGL_INFO, "\nICY Info: %s\n", info); | |
| 110 free(info); | |
| 111 } | |
| 112 } | |
| 113 | |
| 16032 | 114 /** |
| 115 * \brief read data from scast/ultravox stream without any metadata | |
| 116 * \param fd file descriptor to read from | |
| 117 * \param buffer buffer to read data into | |
| 118 * \param size number of bytes to read | |
| 119 * \param sc streaming_ctrl_t whose buffer is consumed before reading from fd | |
| 120 */ | |
| 16013 | 121 static int scast_streaming_read(int fd, char *buffer, int size, |
| 122 streaming_ctrl_t *sc) { | |
| 123 scast_data_t *sd = (scast_data_t *)sc->data; | |
| 124 unsigned block, ret; | |
| 125 unsigned done = 0; | |
| 126 | |
| 127 // first read remaining data up to next metadata | |
| 128 block = sd->metaint - sd->metapos; | |
| 129 if (block > size) | |
| 130 block = size; | |
| 131 ret = my_read(fd, buffer, block, sc); | |
| 132 sd->metapos += ret; | |
| 133 done += ret; | |
| 134 if (ret != block) // read problems or eof | |
| 135 size = done; | |
| 136 | |
| 137 while (done < size) { // now comes the metadata | |
| 138 if (sd->is_ultravox) | |
| 16070 | 139 { |
| 16013 | 140 sd->metaint = uvox_meta_read(fd, sc); |
| 16070 | 141 if (!sd->metaint) |
| 142 size = done; | |
| 143 } | |
| 16013 | 144 else |
| 145 scast_meta_read(fd, sc); // read and display metadata | |
| 146 sd->metapos = 0; | |
| 147 block = size - done; | |
| 148 if (block > sd->metaint) | |
| 149 block = sd->metaint; | |
| 150 ret = my_read(fd, &buffer[done], block, sc); | |
| 151 sd->metapos += ret; | |
| 152 done += ret; | |
| 153 if (ret != block) // read problems or eof | |
| 154 size = done; | |
| 155 } | |
| 156 return done; | |
| 157 } | |
| 158 | |
| 159 static int scast_streaming_start(stream_t *stream) { | |
| 160 int metaint; | |
| 161 scast_data_t *scast_data; | |
| 162 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; | |
| 16070 | 163 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
| 16013 | 164 if (!stream || stream->fd < 0 || !http_hdr) |
| 165 return -1; | |
| 166 if (is_ultravox) | |
| 167 metaint = 0; | |
| 168 else { | |
| 169 metaint = atoi(http_get_field(http_hdr, "Icy-MetaInt")); | |
| 170 if (metaint <= 0) | |
| 171 return -1; | |
| 172 } | |
| 173 stream->streaming_ctrl->buffer = malloc(http_hdr->body_size); | |
| 174 stream->streaming_ctrl->buffer_size = http_hdr->body_size; | |
| 175 stream->streaming_ctrl->buffer_pos = 0; | |
| 176 memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size); | |
| 177 scast_data = malloc(sizeof(scast_data_t)); | |
| 178 scast_data->metaint = metaint; | |
| 179 scast_data->metapos = 0; | |
| 180 scast_data->is_ultravox = is_ultravox; | |
| 181 http_free(http_hdr); | |
| 182 stream->streaming_ctrl->data = scast_data; | |
| 183 stream->streaming_ctrl->streaming_read = scast_streaming_read; | |
| 184 stream->streaming_ctrl->streaming_seek = NULL; | |
| 185 stream->streaming_ctrl->prebuffer_size = 64 * 1024; // 64 KBytes | |
| 186 stream->streaming_ctrl->buffering = 1; | |
| 187 stream->streaming_ctrl->status = streaming_playing_e; | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 15585 | 191 static int nop_streaming_start( stream_t *stream ) { |
| 192 HTTP_header_t *http_hdr = NULL; | |
| 193 char *next_url=NULL; | |
| 194 URL_t *rd_url=NULL; | |
| 195 int fd,ret; | |
| 196 if( stream==NULL ) return -1; | |
| 197 | |
| 198 fd = stream->fd; | |
| 199 if( fd<0 ) { | |
| 200 fd = http_send_request( stream->streaming_ctrl->url, 0 ); | |
| 201 if( fd<0 ) return -1; | |
| 202 http_hdr = http_read_response( fd ); | |
| 203 if( http_hdr==NULL ) return -1; | |
| 204 | |
| 205 switch( http_hdr->status_code ) { | |
| 206 case 200: // OK | |
| 207 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
| 208 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
| 209 if( http_hdr->body_size>0 ) { | |
| 210 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
| 211 http_free( http_hdr ); | |
| 212 return -1; | |
| 213 } | |
| 214 } | |
| 215 break; | |
| 216 // Redirect | |
| 217 case 301: // Permanently | |
| 218 case 302: // Temporarily | |
|
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
219 case 303: // See Other |
| 15585 | 220 ret=-1; |
| 221 next_url = http_get_field( http_hdr, "Location" ); | |
| 222 | |
| 223 if (next_url != NULL) | |
| 224 rd_url=url_new(next_url); | |
| 225 | |
| 226 if (next_url != NULL && rd_url != NULL) { | |
| 227 mp_msg(MSGT_NETWORK,MSGL_STATUS,"Redirected: Using this url instead %s\n",next_url); | |
| 228 stream->streaming_ctrl->url=check4proxies(rd_url); | |
| 229 ret=nop_streaming_start(stream); //recursively get streaming started | |
| 230 } else { | |
| 231 mp_msg(MSGT_NETWORK,MSGL_ERR,"Redirection failed\n"); | |
| 232 closesocket( fd ); | |
| 233 fd = -1; | |
| 234 } | |
| 235 return ret; | |
| 236 break; | |
| 237 case 401: //Authorization required | |
| 238 case 403: //Forbidden | |
| 239 case 404: //Not found | |
| 240 case 500: //Server Error | |
| 241 default: | |
| 242 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned code %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
| 243 closesocket( fd ); | |
| 244 fd = -1; | |
| 245 return -1; | |
| 246 break; | |
| 247 } | |
| 248 stream->fd = fd; | |
| 249 } else { | |
| 250 http_hdr = (HTTP_header_t*)stream->streaming_ctrl->data; | |
| 251 if( http_hdr->body_size>0 ) { | |
| 252 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
| 253 http_free( http_hdr ); | |
| 254 stream->streaming_ctrl->data = NULL; | |
| 255 return -1; | |
| 256 } | |
| 257 } | |
| 258 } | |
| 259 | |
| 260 if( http_hdr ) { | |
| 261 http_free( http_hdr ); | |
| 262 stream->streaming_ctrl->data = NULL; | |
| 263 } | |
| 264 | |
| 265 stream->streaming_ctrl->streaming_read = nop_streaming_read; | |
| 266 stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
| 267 stream->streaming_ctrl->prebuffer_size = 64*1024; // 64 KBytes | |
| 268 stream->streaming_ctrl->buffering = 1; | |
| 269 stream->streaming_ctrl->status = streaming_playing_e; | |
| 270 return 0; | |
| 271 } | |
| 272 | |
| 870 | 273 HTTP_header_t * |
|
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
16948
diff
changeset
|
274 http_new_header(void) { |
| 870 | 275 HTTP_header_t *http_hdr; |
| 276 | |
| 18879 | 277 http_hdr = malloc(sizeof(HTTP_header_t)); |
| 870 | 278 if( http_hdr==NULL ) return NULL; |
| 279 memset( http_hdr, 0, sizeof(HTTP_header_t) ); | |
| 280 | |
| 281 return http_hdr; | |
| 282 } | |
| 283 | |
| 284 void | |
| 285 http_free( HTTP_header_t *http_hdr ) { | |
| 3039 | 286 HTTP_field_t *field, *field2free; |
| 870 | 287 if( http_hdr==NULL ) return; |
| 288 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol ); | |
| 289 if( http_hdr->uri!=NULL ) free( http_hdr->uri ); | |
| 290 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase ); | |
| 291 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search ); | |
| 902 | 292 if( http_hdr->method!=NULL ) free( http_hdr->method ); |
| 293 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer ); | |
| 3039 | 294 field = http_hdr->first_field; |
| 295 while( field!=NULL ) { | |
| 296 field2free = field; | |
| 14460 | 297 if (field->field_name) |
| 298 free(field->field_name); | |
| 3039 | 299 field = field->next; |
| 300 free( field2free ); | |
| 301 } | |
| 870 | 302 free( http_hdr ); |
| 3039 | 303 http_hdr = NULL; |
| 870 | 304 } |
| 305 | |
| 902 | 306 int |
| 307 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) { | |
| 1027 | 308 if( http_hdr==NULL || response==NULL || length<0 ) return -1; |
|
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
309 |
|
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
310 if( (unsigned)length > SIZE_MAX - http_hdr->buffer_size - 1) { |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
311 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Bad size in memory (re)allocation\n"); |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
312 return -1; |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
313 } |
| 7293 | 314 http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 ); |
|
7304
7da2c2a68547
Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents:
7293
diff
changeset
|
315 if( http_hdr->buffer==NULL ) { |
| 7293 | 316 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n"); |
| 902 | 317 return -1; |
| 318 } | |
| 7293 | 319 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length ); |
| 320 http_hdr->buffer_size += length; | |
| 321 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string! | |
| 902 | 322 return http_hdr->buffer_size; |
| 323 } | |
| 324 | |
| 325 int | |
|
2489
0ecc1b4f7cf8
Added ASF http server streaming (Not mms streaming).
bertrand
parents:
2310
diff
changeset
|
326 http_is_header_entire( HTTP_header_t *http_hdr ) { |
| 902 | 327 if( http_hdr==NULL ) return -1; |
| 7293 | 328 if( http_hdr->buffer==NULL ) return 0; // empty |
| 329 | |
| 3784 | 330 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL && |
| 331 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0; | |
| 332 return 1; | |
| 902 | 333 } |
| 334 | |
| 335 int | |
| 336 http_response_parse( HTTP_header_t *http_hdr ) { | |
| 870 | 337 char *hdr_ptr, *ptr; |
| 338 char *field=NULL; | |
|
18558
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
339 int pos_hdr_sep, hdr_sep_len; |
|
4928dd61f136
Fix potential integer overflows in memory allocation.
rtogni
parents:
18094
diff
changeset
|
340 size_t len; |
| 902 | 341 if( http_hdr==NULL ) return -1; |
| 342 if( http_hdr->is_parsed ) return 0; | |
| 870 | 343 |
| 344 // Get the protocol | |
| 902 | 345 hdr_ptr = strstr( http_hdr->buffer, " " ); |
| 870 | 346 if( hdr_ptr==NULL ) { |
| 5915 | 347 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n"); |
| 902 | 348 return -1; |
| 870 | 349 } |
| 902 | 350 len = hdr_ptr-http_hdr->buffer; |
| 18879 | 351 http_hdr->protocol = malloc(len+1); |
| 870 | 352 if( http_hdr->protocol==NULL ) { |
| 5915 | 353 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 902 | 354 return -1; |
| 870 | 355 } |
| 902 | 356 strncpy( http_hdr->protocol, http_hdr->buffer, len ); |
| 357 http_hdr->protocol[len]='\0'; | |
| 870 | 358 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) { |
| 359 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) { | |
| 5915 | 360 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n"); |
| 902 | 361 return -1; |
| 870 | 362 } |
| 363 } | |
| 364 | |
| 365 // Get the status code | |
| 366 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) { | |
| 5915 | 367 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n"); |
| 902 | 368 return -1; |
| 870 | 369 } |
| 370 hdr_ptr += 4; | |
| 371 | |
| 372 // Get the reason phrase | |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
373 ptr = strstr( hdr_ptr, "\n" ); |
| 870 | 374 if( hdr_ptr==NULL ) { |
| 5915 | 375 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n"); |
| 902 | 376 return -1; |
| 870 | 377 } |
| 378 len = ptr-hdr_ptr; | |
| 18879 | 379 http_hdr->reason_phrase = malloc(len+1); |
| 870 | 380 if( http_hdr->reason_phrase==NULL ) { |
| 5915 | 381 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 902 | 382 return -1; |
| 870 | 383 } |
| 384 strncpy( http_hdr->reason_phrase, hdr_ptr, len ); | |
| 4311 | 385 if( http_hdr->reason_phrase[len-1]=='\r' ) { |
| 386 len--; | |
| 387 } | |
| 870 | 388 http_hdr->reason_phrase[len]='\0'; |
| 389 | |
| 390 // Set the position of the header separator: \r\n\r\n | |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
391 hdr_sep_len = 4; |
| 902 | 392 ptr = strstr( http_hdr->buffer, "\r\n\r\n" ); |
| 870 | 393 if( ptr==NULL ) { |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
394 ptr = strstr( http_hdr->buffer, "\n\n" ); |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
395 if( ptr==NULL ) { |
| 5915 | 396 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n"); |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
397 return -1; |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
398 } |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
399 hdr_sep_len = 2; |
| 870 | 400 } |
| 902 | 401 pos_hdr_sep = ptr-http_hdr->buffer; |
| 870 | 402 |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
403 // Point to the first line after the method line. |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
404 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1; |
| 870 | 405 do { |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
406 ptr = hdr_ptr; |
|
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
407 while( *ptr!='\r' && *ptr!='\n' ) ptr++; |
| 870 | 408 len = ptr-hdr_ptr; |
|
4816
f1dea39a50bb
Fixed the http response parser when the http header only has the HTTP
bertrand
parents:
4311
diff
changeset
|
409 if( len==0 ) break; |
| 870 | 410 field = (char*)realloc(field, len+1); |
| 411 if( field==NULL ) { | |
| 5915 | 412 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
| 902 | 413 return -1; |
| 870 | 414 } |
| 415 strncpy( field, hdr_ptr, len ); | |
| 416 field[len]='\0'; | |
| 417 http_set_field( http_hdr, field ); | |
|
3514
43518985def8
Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents:
3497
diff
changeset
|
418 hdr_ptr = ptr+((*ptr=='\r')?2:1); |
| 902 | 419 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) ); |
| 870 | 420 |
| 421 if( field!=NULL ) free( field ); | |
| 422 | |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
423 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) { |
| 870 | 424 // Response has data! |
|
8179
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
425 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len; |
|
63a5e03f4346
Removed hard coded value for the length of the header separator.
bertrand
parents:
7304
diff
changeset
|
426 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len); |
| 870 | 427 } |
| 428 | |
| 902 | 429 http_hdr->is_parsed = 1; |
| 430 return 0; | |
| 870 | 431 } |
| 432 | |
| 433 char * | |
| 902 | 434 http_build_request( HTTP_header_t *http_hdr ) { |
| 3497 | 435 char *ptr, *uri=NULL; |
| 902 | 436 int len; |
| 3039 | 437 HTTP_field_t *field; |
| 870 | 438 if( http_hdr==NULL ) return NULL; |
| 439 | |
| 440 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET"); | |
| 441 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/"); | |
| 3497 | 442 else { |
| 18879 | 443 uri = malloc(strlen(http_hdr->uri) + 1); |
| 3497 | 444 if( uri==NULL ) { |
| 5915 | 445 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
| 3497 | 446 return NULL; |
| 447 } | |
| 12391 | 448 strcpy(uri,http_hdr->uri); |
| 3497 | 449 } |
| 870 | 450 |
| 3497 | 451 //**** Compute the request length |
| 452 // Add the Method line | |
| 453 len = strlen(http_hdr->method)+strlen(uri)+12; | |
| 454 // Add the fields | |
| 455 field = http_hdr->first_field; | |
| 3039 | 456 while( field!=NULL ) { |
| 457 len += strlen(field->field_name)+2; | |
| 458 field = field->next; | |
| 459 } | |
| 3497 | 460 // Add the CRLF |
| 461 len += 2; | |
| 462 // Add the body | |
| 902 | 463 if( http_hdr->body!=NULL ) { |
| 464 len += http_hdr->body_size; | |
| 465 } | |
| 3497 | 466 // Free the buffer if it was previously used |
| 902 | 467 if( http_hdr->buffer!=NULL ) { |
| 468 free( http_hdr->buffer ); | |
| 469 http_hdr->buffer = NULL; | |
| 470 } | |
| 18879 | 471 http_hdr->buffer = malloc(len+1); |
| 902 | 472 if( http_hdr->buffer==NULL ) { |
| 5915 | 473 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n"); |
| 902 | 474 return NULL; |
| 475 } | |
| 476 http_hdr->buffer_size = len; | |
| 477 | |
| 3497 | 478 //*** Building the request |
| 902 | 479 ptr = http_hdr->buffer; |
| 3497 | 480 // Add the method line |
| 481 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); | |
| 3039 | 482 field = http_hdr->first_field; |
| 3497 | 483 // Add the field |
| 3039 | 484 while( field!=NULL ) { |
| 485 ptr += sprintf( ptr, "%s\r\n", field->field_name ); | |
| 486 field = field->next; | |
| 487 } | |
| 870 | 488 ptr += sprintf( ptr, "\r\n" ); |
| 3497 | 489 // Add the body |
| 870 | 490 if( http_hdr->body!=NULL ) { |
| 491 memcpy( ptr, http_hdr->body, http_hdr->body_size ); | |
| 492 } | |
| 3497 | 493 |
| 494 if( uri ) free( uri ); | |
| 902 | 495 return http_hdr->buffer; |
| 870 | 496 } |
| 497 | |
| 498 char * | |
| 499 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { | |
| 500 if( http_hdr==NULL || field_name==NULL ) return NULL; | |
| 3039 | 501 http_hdr->field_search_pos = http_hdr->first_field; |
| 502 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); | |
| 870 | 503 if( http_hdr->field_search==NULL ) { |
| 5915 | 504 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 870 | 505 return NULL; |
| 506 } | |
| 507 strcpy( http_hdr->field_search, field_name ); | |
| 508 return http_get_next_field( http_hdr ); | |
| 509 } | |
| 510 | |
| 511 char * | |
| 512 http_get_next_field( HTTP_header_t *http_hdr ) { | |
| 513 char *ptr; | |
| 3039 | 514 HTTP_field_t *field; |
| 870 | 515 if( http_hdr==NULL ) return NULL; |
| 516 | |
| 3039 | 517 field = http_hdr->field_search_pos; |
| 518 while( field!=NULL ) { | |
| 519 ptr = strstr( field->field_name, ":" ); | |
| 870 | 520 if( ptr==NULL ) return NULL; |
| 3039 | 521 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { |
| 870 | 522 ptr++; // Skip the column |
| 523 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some | |
| 3039 | 524 http_hdr->field_search_pos = field->next; |
| 870 | 525 return ptr; // return the value without the field name |
| 526 } | |
| 3039 | 527 field = field->next; |
| 870 | 528 } |
| 529 return NULL; | |
| 530 } | |
| 531 | |
| 532 void | |
| 3039 | 533 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) { |
| 534 HTTP_field_t *new_field; | |
| 535 if( http_hdr==NULL || field_name==NULL ) return; | |
| 870 | 536 |
| 18879 | 537 new_field = malloc(sizeof(HTTP_field_t)); |
| 3039 | 538 if( new_field==NULL ) { |
| 5915 | 539 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 870 | 540 return; |
| 541 } | |
| 3039 | 542 new_field->next = NULL; |
| 18879 | 543 new_field->field_name = malloc(strlen(field_name)+1); |
| 3039 | 544 if( new_field->field_name==NULL ) { |
| 5915 | 545 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 3039 | 546 return; |
| 547 } | |
| 548 strcpy( new_field->field_name, field_name ); | |
| 549 | |
| 550 if( http_hdr->last_field==NULL ) { | |
| 551 http_hdr->first_field = new_field; | |
| 552 } else { | |
| 553 http_hdr->last_field->next = new_field; | |
| 554 } | |
| 555 http_hdr->last_field = new_field; | |
| 870 | 556 http_hdr->field_nb++; |
| 557 } | |
| 558 | |
| 559 void | |
| 560 http_set_method( HTTP_header_t *http_hdr, const char *method ) { | |
| 561 if( http_hdr==NULL || method==NULL ) return; | |
| 562 | |
| 18879 | 563 http_hdr->method = malloc(strlen(method)+1); |
| 870 | 564 if( http_hdr->method==NULL ) { |
| 5915 | 565 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 870 | 566 return; |
| 567 } | |
| 568 strcpy( http_hdr->method, method ); | |
| 569 } | |
| 570 | |
| 571 void | |
| 572 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) { | |
| 573 if( http_hdr==NULL || uri==NULL ) return; | |
| 574 | |
| 18879 | 575 http_hdr->uri = malloc(strlen(uri)+1); |
| 870 | 576 if( http_hdr->uri==NULL ) { |
| 5915 | 577 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); |
| 870 | 578 return; |
| 579 } | |
| 580 strcpy( http_hdr->uri, uri ); | |
| 581 } | |
| 582 | |
| 6514 | 583 int |
| 584 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
585 char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL; |
| 6514 | 586 int encoded_len, pass_len=0, out_len; |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
587 int res = -1; |
| 6514 | 588 if( http_hdr==NULL || username==NULL ) return -1; |
| 589 | |
| 590 if( password!=NULL ) { | |
| 591 pass_len = strlen(password); | |
| 592 } | |
| 593 | |
| 18879 | 594 usr_pass = malloc(strlen(username)+pass_len+2); |
| 6514 | 595 if( usr_pass==NULL ) { |
| 596 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
597 goto out; |
| 6514 | 598 } |
| 599 | |
| 600 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); | |
| 601 | |
| 602 // Base 64 encode with at least 33% more data than the original size | |
| 603 encoded_len = strlen(usr_pass)*2; | |
| 18879 | 604 b64_usr_pass = malloc(encoded_len); |
| 6514 | 605 if( b64_usr_pass==NULL ) { |
| 606 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
607 goto out; |
| 6514 | 608 } |
| 609 | |
| 610 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); | |
| 611 if( out_len<0 ) { | |
| 612 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
613 goto out; |
| 6514 | 614 } |
| 615 | |
| 616 b64_usr_pass[out_len]='\0'; | |
| 617 | |
| 18879 | 618 auth = malloc(encoded_len+22); |
| 6514 | 619 if( auth==NULL ) { |
| 620 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
621 goto out; |
| 6514 | 622 } |
| 623 | |
| 624 sprintf( auth, "Authorization: Basic %s", b64_usr_pass); | |
| 625 http_set_field( http_hdr, auth ); | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
626 res = 0; |
| 6514 | 627 |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
628 out: |
| 6514 | 629 free( usr_pass ); |
| 630 free( b64_usr_pass ); | |
| 631 free( auth ); | |
| 632 | |
|
18094
16f2bcd5d148
free memory on error in http_add_basic_authentication
reimar
parents:
17932
diff
changeset
|
633 return res; |
| 6514 | 634 } |
| 635 | |
| 870 | 636 void |
| 637 http_debug_hdr( HTTP_header_t *http_hdr ) { | |
| 3039 | 638 HTTP_field_t *field; |
| 639 int i = 0; | |
| 902 | 640 if( http_hdr==NULL ) return; |
| 870 | 641 |
| 5915 | 642 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); |
| 643 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); | |
| 644 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); | |
| 645 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); | |
| 646 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); | |
| 647 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); | |
| 648 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); | |
| 649 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); | |
| 870 | 650 |
| 5915 | 651 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); |
| 3039 | 652 field = http_hdr->first_field; |
| 653 while( field!=NULL ) { | |
| 5915 | 654 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); |
| 3039 | 655 field = field->next; |
| 656 } | |
| 5915 | 657 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n"); |
| 870 | 658 } |
| 6514 | 659 |
| 660 int | |
| 661 base64_encode(const void *enc, int encLen, char *out, int outMax) { | |
|
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
662 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 6514 | 663 |
| 664 unsigned char *encBuf; | |
| 665 int outLen; | |
| 666 unsigned int bits; | |
| 667 unsigned int shift; | |
| 668 | |
| 669 encBuf = (unsigned char*)enc; | |
| 670 outLen = 0; | |
| 671 bits = 0; | |
| 672 shift = 0; | |
|
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
673 outMax &= ~3; |
| 6514 | 674 |
| 22031 | 675 while(1) { |
| 6514 | 676 if( encLen>0 ) { |
| 677 // Shift in byte | |
| 678 bits <<= 8; | |
| 679 bits |= *encBuf; | |
| 680 shift += 8; | |
| 681 // Next byte | |
| 682 encBuf++; | |
| 683 encLen--; | |
| 684 } else if( shift>0 ) { | |
| 685 // Pad last bits to 6 bits - will end next loop | |
| 686 bits <<= 6 - shift; | |
| 687 shift = 6; | |
| 688 } else { | |
|
17778
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
689 // As per RFC 2045, section 6.8, |
|
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
690 // pad output as necessary: 0 to 2 '=' chars. |
|
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
691 while( outLen & 3 ){ |
|
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
692 *out++ = '='; |
|
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
693 outLen++; |
|
37bfcf89c89c
Fix base64 encoding for basic auth according to RFC.
reimar
parents:
17566
diff
changeset
|
694 } |
| 6514 | 695 |
| 696 return outLen; | |
| 697 } | |
| 698 | |
| 699 // Encode 6 bit segments | |
| 700 while( shift>=6 ) { | |
| 22031 | 701 if (outLen >= outMax) |
| 702 return -1; | |
| 6514 | 703 shift -= 6; |
| 704 *out = b64[ (bits >> shift) & 0x3F ]; | |
| 705 out++; | |
| 706 outLen++; | |
| 707 } | |
| 708 } | |
| 709 } | |
| 710 | |
|
25970
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
711 static void print_icy_metadata(HTTP_header_t *http_hdr) { |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
712 const char *field_data; |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
713 // note: I skip icy-notice1 and 2, as they contain html <BR> |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
714 // and are IMHO useless info ::atmos |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
715 if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
716 mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
717 if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
718 mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
719 if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
720 mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
721 // XXX: does this really mean public server? ::atmos |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
722 if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
723 mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
724 if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
725 mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
726 } |
|
c6ec51cc3b13
Move printing of Icy-Metadata into an extra function
reimar
parents:
25969
diff
changeset
|
727 |
| 21567 | 728 //! If this function succeeds you must closesocket stream->fd |
| 15585 | 729 static int http_streaming_start(stream_t *stream, int* file_format) { |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
730 HTTP_header_t *http_hdr = NULL; |
| 15585 | 731 unsigned int i; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
732 int fd = stream->fd; |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
733 int res = STREAM_UNSUPPORTED; |
| 15585 | 734 int redirect = 0; |
| 735 int auth_retry=0; | |
| 736 int seekable=0; | |
| 737 char *content_type; | |
| 738 char *next_url; | |
| 739 URL_t *url = stream->streaming_ctrl->url; | |
| 6514 | 740 |
| 15585 | 741 do |
| 742 { | |
|
21541
3b4ed8857b38
Fix potential endless loop in http_streaming_start due
reimar
parents:
21540
diff
changeset
|
743 redirect = 0; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
744 if (fd > 0) closesocket(fd); |
| 15585 | 745 fd = http_send_request( url, 0 ); |
| 746 if( fd<0 ) { | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
747 goto err_out; |
| 15585 | 748 } |
| 749 | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
750 http_free(http_hdr); |
| 15585 | 751 http_hdr = http_read_response( fd ); |
| 752 if( http_hdr==NULL ) { | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
753 goto err_out; |
| 15585 | 754 } |
| 755 | |
| 17932 | 756 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { |
| 15585 | 757 http_debug_hdr( http_hdr ); |
| 758 } | |
| 759 | |
| 760 // Check if we can make partial content requests and thus seek in http-streams | |
| 761 if( http_hdr!=NULL && http_hdr->status_code==200 ) { | |
| 762 char *accept_ranges; | |
| 763 if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) | |
| 764 seekable = strncmp(accept_ranges,"bytes",5)==0; | |
| 765 } | |
| 766 | |
|
25971
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
767 print_icy_metadata(http_hdr); |
|
64b1e4ea04fc
Always display Icy-Metadata if available, whether we recognize an ICY-Server
reimar
parents:
25970
diff
changeset
|
768 |
| 15585 | 769 // Check if the response is an ICY status_code reason_phrase |
|
25968
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
770 if( !strcasecmp(http_hdr->protocol, "ICY") || |
|
c7f41f9e2eb8
Detect IceCast also by Icy-MetaInt header part in http_streaming_start(),
reimar
parents:
25246
diff
changeset
|
771 http_get_field(http_hdr, "Icy-MetaInt") ) { |
| 15585 | 772 switch( http_hdr->status_code ) { |
| 773 case 200: { // OK | |
| 25969 | 774 char *field_data; |
| 15585 | 775 // If content-type == video/nsv we most likely have a winamp video stream |
| 776 // otherwise it should be mp3. if there are more types consider adding mime type | |
| 777 // handling like later | |
| 778 if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) | |
| 779 *file_format = DEMUXER_TYPE_NSV; | |
|
16948
9b7925705f5b
Add another content-type for aac audio in shoutcast streams
rtognimp
parents:
16932
diff
changeset
|
780 else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) |
|
16917
c45409728a9d
Use correct demuxer type for aac in shoutcast streams
rtognimp
parents:
16614
diff
changeset
|
781 *file_format = DEMUXER_TYPE_AAC; |
| 15585 | 782 else |
| 783 *file_format = DEMUXER_TYPE_AUDIO; | |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
784 res = STREAM_ERROR; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
785 goto out; |
| 15585 | 786 } |
| 787 case 400: // Server Full | |
| 788 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
789 goto err_out; |
| 15585 | 790 case 401: // Service Unavailable |
| 791 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
792 goto err_out; |
| 15585 | 793 case 403: // Service Forbidden |
| 794 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
795 goto err_out; |
| 15585 | 796 case 404: // Resource Not Found |
| 797 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
798 goto err_out; |
| 15585 | 799 default: |
| 800 mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
801 goto err_out; |
| 15585 | 802 } |
| 803 } | |
| 804 | |
| 805 // Assume standard http if not ICY | |
| 806 switch( http_hdr->status_code ) { | |
| 807 case 200: // OK | |
| 808 // Look if we can use the Content-Type | |
| 809 content_type = http_get_field( http_hdr, "Content-Type" ); | |
| 810 if( content_type!=NULL ) { | |
| 811 char *content_length = NULL; | |
| 812 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); | |
|
25999
aff1f94af7ba
Fill stream->end_pos if possible, fixing lavf demuxers that need to seek.
albeu
parents:
25972
diff
changeset
|
813 if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) { |
| 15585 | 814 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); |
|
25999
aff1f94af7ba
Fill stream->end_pos if possible, fixing lavf demuxers that need to seek.
albeu
parents:
25972
diff
changeset
|
815 stream->end_pos = atoi(content_length); |
|
aff1f94af7ba
Fill stream->end_pos if possible, fixing lavf demuxers that need to seek.
albeu
parents:
25972
diff
changeset
|
816 } |
| 15585 | 817 // Check in the mime type table for a demuxer type |
| 818 i = 0; | |
| 819 while(mime_type_table[i].mime_type != NULL) { | |
| 820 if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { | |
| 821 *file_format = mime_type_table[i].demuxer_type; | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
822 res = seekable; |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
823 goto out; |
| 15585 | 824 } |
| 825 i++; | |
| 826 } | |
| 827 } | |
| 828 // Not found in the mime type table, don't fail, | |
| 829 // we should try raw HTTP | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
830 res = seekable; |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
831 goto out; |
| 15585 | 832 // Redirect |
| 833 case 301: // Permanently | |
| 834 case 302: // Temporarily | |
|
19459
6dad4b1efb82
Handle 303 (See Other) redirect, part of a patch by Benjamin Zores (ben at geexbox org)
reimar
parents:
19312
diff
changeset
|
835 case 303: // See Other |
| 15585 | 836 // TODO: RFC 2616, recommand to detect infinite redirection loops |
| 837 next_url = http_get_field( http_hdr, "Location" ); | |
| 838 if( next_url!=NULL ) { | |
|
25238
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
839 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
|
20784
720206eef78b
Support URL redirections that do not specify full URL.
reimar
parents:
20185
diff
changeset
|
840 stream->streaming_ctrl->url = url_redirect( &url, next_url ); |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
841 if (!strcasecmp(url->protocol, "mms")) { |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
842 res = STREAM_REDIRECTED; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
843 goto err_out; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
844 } |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
845 if (strcasecmp(url->protocol, "http")) { |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
846 mp_msg(MSGT_NETWORK,MSGL_ERR,"Unsupported http %d redirect to %s protocol\n", http_hdr->status_code, url->protocol); |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
847 goto err_out; |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
848 } |
|
25238
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
849 if (is_ultravox) { |
|
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
850 free(url->protocol); |
|
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
851 url->protocol = strdup("unsv"); |
|
f42b8e689416
Preserve unsv:// protocol specifier over http redirects.
reimar
parents:
25211
diff
changeset
|
852 } |
| 15585 | 853 redirect = 1; |
| 854 } | |
| 855 break; | |
| 856 case 401: // Authentication required | |
|
21566
79d969f9eec0
STREAM_UNSUPPORTED is -1, so use the former for return value in all places.
reimar
parents:
21565
diff
changeset
|
857 if( http_authenticate(http_hdr, url, &auth_retry)<0 ) |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
858 goto err_out; |
| 15585 | 859 redirect = 1; |
| 860 break; | |
| 861 default: | |
| 862 mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
863 goto err_out; |
| 15585 | 864 } |
| 865 } while( redirect ); | |
| 866 | |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
867 err_out: |
|
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
868 if (fd > 0) closesocket( fd ); |
|
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
869 fd = -1; |
| 21542 | 870 http_free( http_hdr ); |
| 21776 | 871 http_hdr = NULL; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
872 out: |
| 21776 | 873 stream->streaming_ctrl->data = (void*)http_hdr; |
|
21565
546cf4a6377a
Make sure stream->fd is set correct (esp. to -1 on error when fd is closed)
reimar
parents:
21542
diff
changeset
|
874 stream->fd = fd; |
|
21540
147c1c305f21
Fix lots and lots of potential memory/fd leaks in http_streaming_start
reimar
parents:
20784
diff
changeset
|
875 return res; |
| 15585 | 876 } |
| 877 | |
| 878 static int fixup_open(stream_t *stream,int seekable) { | |
| 16013 | 879 HTTP_header_t *http_hdr = stream->streaming_ctrl->data; |
|
16078
095e980cf7c0
Some ICY servers (e.g. http://broadcast.spnet.net:8000/darikhigh) do not set
reimar
parents:
16070
diff
changeset
|
880 int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); |
| 16070 | 881 int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; |
| 15585 | 882 |
| 883 stream->type = STREAMTYPE_STREAM; | |
| 16013 | 884 if(!is_icy && !is_ultravox && seekable) |
| 15585 | 885 { |
| 886 stream->flags |= STREAM_SEEK; | |
| 887 stream->seek = http_seek; | |
| 888 } | |
| 889 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
| 16013 | 890 if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) |
| 15585 | 891 if(nop_streaming_start( stream )) { |
| 892 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); | |
| 21567 | 893 if (stream->fd >= 0) |
| 894 closesocket(stream->fd); | |
| 895 stream->fd = -1; | |
| 15585 | 896 streaming_ctrl_free(stream->streaming_ctrl); |
| 897 stream->streaming_ctrl = NULL; | |
| 24257 | 898 return STREAM_UNSUPPORTED; |
| 15585 | 899 } |
| 900 | |
| 901 fixup_network_stream_cache(stream); | |
| 902 return STREAM_OK; | |
| 903 } | |
| 904 | |
| 905 static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { | |
| 906 int seekable=0; | |
| 907 URL_t *url; | |
| 908 | |
| 909 stream->streaming_ctrl = streaming_ctrl_new(); | |
| 910 if( stream->streaming_ctrl==NULL ) { | |
| 911 return STREAM_ERROR; | |
| 912 } | |
| 913 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
| 914 url = url_new(stream->url); | |
| 915 stream->streaming_ctrl->url = check4proxies(url); | |
| 16417 | 916 url_free(url); |
| 15585 | 917 |
| 20185 | 918 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url); |
| 15585 | 919 seekable = http_streaming_start(stream, file_format); |
| 920 if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { | |
| 21567 | 921 if (stream->fd >= 0) |
| 922 closesocket(stream->fd); | |
| 923 stream->fd = -1; | |
|
25135
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
924 if (seekable == STREAM_REDIRECTED) |
|
66f628d13442
Support stream redirection from http to mms, fix bug #927.
ulion
parents:
24257
diff
changeset
|
925 return seekable; |
| 15585 | 926 streaming_ctrl_free(stream->streaming_ctrl); |
| 927 stream->streaming_ctrl = NULL; | |
| 24257 | 928 return STREAM_UNSUPPORTED; |
| 15585 | 929 } |
| 930 | |
| 931 return fixup_open(stream, seekable); | |
| 932 } | |
| 933 | |
| 934 static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { | |
| 935 int seekable=0; | |
| 936 URL_t *url; | |
| 937 | |
| 938 stream->streaming_ctrl = streaming_ctrl_new(); | |
| 939 if( stream->streaming_ctrl==NULL ) { | |
| 940 return STREAM_ERROR; | |
| 941 } | |
| 942 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
| 943 url = url_new(stream->url); | |
| 944 stream->streaming_ctrl->url = check4proxies(url); | |
| 16614 | 945 url_free(url); |
| 15585 | 946 |
| 20185 | 947 mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url); |
| 15585 | 948 seekable = http_streaming_start(stream, file_format); |
| 949 if(seekable < 0) { | |
| 21567 | 950 if (stream->fd >= 0) |
| 951 closesocket(stream->fd); | |
| 952 stream->fd = -1; | |
| 15585 | 953 streaming_ctrl_free(stream->streaming_ctrl); |
| 954 stream->streaming_ctrl = NULL; | |
| 24257 | 955 return STREAM_UNSUPPORTED; |
| 15585 | 956 } |
| 957 | |
| 958 return fixup_open(stream, seekable); | |
| 959 } | |
| 960 | |
| 961 | |
| 25211 | 962 const stream_info_t stream_info_http1 = { |
| 15585 | 963 "http streaming", |
| 964 "null", | |
| 965 "Bertrand, Albeau, Reimar Doeffinger, Arpi?", | |
| 966 "plain http", | |
| 967 open_s1, | |
| 25972 | 968 {"http", "http_proxy", "unsv", "icyx", NULL}, |
| 15585 | 969 NULL, |
| 970 0 // Urls are an option string | |
| 971 }; | |
| 972 | |
| 25211 | 973 const stream_info_t stream_info_http2 = { |
| 15585 | 974 "http streaming", |
| 975 "null", | |
| 976 "Bertrand, Albeu, Arpi? who?", | |
|
16932
c30e0970250c
fix typos: aslo->also, falback->fallback (they were just too annoying *g*)
reimar
parents:
16917
diff
changeset
|
977 "plain http, also used as fallback for many other protocols", |
| 15585 | 978 open_s2, |
| 979 {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback | |
| 980 NULL, | |
| 981 0 // Urls are an option string | |
| 982 }; |
