📄 http.c
字号:
//*** Building the request ptr = http_hdr->buffer; // Add the method line ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version ); field = http_hdr->first_field; // Add the field while( field!=NULL ) { ptr += sprintf( ptr, "%s\r\n", field->field_name ); field = field->next; } ptr += sprintf( ptr, "\r\n" ); // Add the body if( http_hdr->body!=NULL ) { memcpy( ptr, http_hdr->body, http_hdr->body_size ); } if( uri ) free( uri ); return http_hdr->buffer; }char *http_get_field( HTTP_header_t *http_hdr, const char *field_name ) { if( http_hdr==NULL || field_name==NULL ) return NULL; http_hdr->field_search_pos = http_hdr->first_field; http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 ); if( http_hdr->field_search==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return NULL; } strcpy( http_hdr->field_search, field_name ); return http_get_next_field( http_hdr );}char *http_get_next_field( HTTP_header_t *http_hdr ) { char *ptr; HTTP_field_t *field; if( http_hdr==NULL ) return NULL; field = http_hdr->field_search_pos; while( field!=NULL ) { ptr = strstr( field->field_name, ":" ); if( ptr==NULL ) return NULL; if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) { ptr++; // Skip the column while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some http_hdr->field_search_pos = field->next; return ptr; // return the value without the field name } field = field->next; } return NULL;}voidhttp_set_field( HTTP_header_t *http_hdr, const char *field_name ) { HTTP_field_t *new_field; if( http_hdr==NULL || field_name==NULL ) return; new_field = malloc(sizeof(HTTP_field_t)); if( new_field==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return; } new_field->next = NULL; new_field->field_name = malloc(strlen(field_name)+1); if( new_field->field_name==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return; } strcpy( new_field->field_name, field_name ); if( http_hdr->last_field==NULL ) { http_hdr->first_field = new_field; } else { http_hdr->last_field->next = new_field; } http_hdr->last_field = new_field; http_hdr->field_nb++;}voidhttp_set_method( HTTP_header_t *http_hdr, const char *method ) { if( http_hdr==NULL || method==NULL ) return; http_hdr->method = malloc(strlen(method)+1); if( http_hdr->method==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return; } strcpy( http_hdr->method, method );}voidhttp_set_uri( HTTP_header_t *http_hdr, const char *uri ) { if( http_hdr==NULL || uri==NULL ) return; http_hdr->uri = malloc(strlen(uri)+1); if( http_hdr->uri==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); return; } strcpy( http_hdr->uri, uri );}inthttp_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) { char *auth = NULL, *usr_pass = NULL, *b64_usr_pass = NULL; int encoded_len, pass_len=0, out_len; int res = -1; if( http_hdr==NULL || username==NULL ) return -1; if( password!=NULL ) { pass_len = strlen(password); } usr_pass = malloc(strlen(username)+pass_len+2); if( usr_pass==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); goto out; } sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password ); // Base 64 encode with at least 33% more data than the original size encoded_len = strlen(usr_pass)*2; b64_usr_pass = malloc(encoded_len); if( b64_usr_pass==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); goto out; } out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len); if( out_len<0 ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n"); goto out; } b64_usr_pass[out_len]='\0'; auth = malloc(encoded_len+22); if( auth==NULL ) { mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n"); goto out; } sprintf( auth, "Authorization: Basic %s", b64_usr_pass); http_set_field( http_hdr, auth ); res = 0; out: free( usr_pass ); free( b64_usr_pass ); free( auth ); return res;}voidhttp_debug_hdr( HTTP_header_t *http_hdr ) { HTTP_field_t *field; int i = 0; if( http_hdr==NULL ) return; mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n"); mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol ); mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version ); mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri ); mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method ); mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code ); mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase ); mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size ); mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n"); field = http_hdr->first_field; while( field!=NULL ) { mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name ); field = field->next; } mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");}int base64_encode(const void *enc, int encLen, char *out, int outMax) { static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; unsigned char *encBuf; int outLen; unsigned int bits; unsigned int shift; encBuf = (unsigned char*)enc; outLen = 0; bits = 0; shift = 0; outMax &= ~3; while(1) { if( encLen>0 ) { // Shift in byte bits <<= 8; bits |= *encBuf; shift += 8; // Next byte encBuf++; encLen--; } else if( shift>0 ) { // Pad last bits to 6 bits - will end next loop bits <<= 6 - shift; shift = 6; } else { // As per RFC 2045, section 6.8, // pad output as necessary: 0 to 2 '=' chars. while( outLen & 3 ){ *out++ = '='; outLen++; } return outLen; } // Encode 6 bit segments while( shift>=6 ) { if (outLen >= outMax) return -1; shift -= 6; *out = b64[ (bits >> shift) & 0x3F ]; out++; outLen++; } }}//! If this function succeeds you must closesocket stream->fdstatic int http_streaming_start(stream_t *stream, int* file_format) { HTTP_header_t *http_hdr = NULL; unsigned int i; int fd = stream->fd; int res = 0; int redirect = 0; int auth_retry=0; int seekable=0; char *content_type; char *next_url; URL_t *url = stream->streaming_ctrl->url; do { redirect = 0; if (fd > 0) closesocket(fd); fd = http_send_request( url, 0 ); if( fd<0 ) { goto err_out; } http_free(http_hdr); http_hdr = http_read_response( fd ); if( http_hdr==NULL ) { goto err_out; } if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) { http_debug_hdr( http_hdr ); } // Check if we can make partial content requests and thus seek in http-streams if( http_hdr!=NULL && http_hdr->status_code==200 ) { char *accept_ranges; if( (accept_ranges = http_get_field(http_hdr,"Accept-Ranges")) != NULL ) seekable = strncmp(accept_ranges,"bytes",5)==0; } // Check if the response is an ICY status_code reason_phrase if( !strcasecmp(http_hdr->protocol, "ICY") ) { switch( http_hdr->status_code ) { case 200: { // OK char *field_data = NULL; // note: I skip icy-notice1 and 2, as they contain html <BR> // and are IMHO useless info ::atmos if( (field_data = http_get_field(http_hdr, "icy-name")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Name : %s\n", field_data); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-genre")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Genre : %s\n", field_data); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-url")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Website: %s\n", field_data); field_data = NULL; // XXX: does this really mean public server? ::atmos if( (field_data = http_get_field(http_hdr, "icy-pub")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Public : %s\n", atoi(field_data)?"yes":"no"); field_data = NULL; if( (field_data = http_get_field(http_hdr, "icy-br")) != NULL ) mp_msg(MSGT_NETWORK,MSGL_INFO,"Bitrate: %skbit/s\n", field_data); field_data = NULL; // If content-type == video/nsv we most likely have a winamp video stream // otherwise it should be mp3. if there are more types consider adding mime type // handling like later if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "video/nsv") || !strcmp(field_data, "misc/ultravox"))) *file_format = DEMUXER_TYPE_NSV; else if ( (field_data = http_get_field(http_hdr, "content-type")) != NULL && (!strcmp(field_data, "audio/aacp") || !strcmp(field_data, "audio/aac"))) *file_format = DEMUXER_TYPE_AAC; else *file_format = DEMUXER_TYPE_AUDIO; goto out; } case 400: // Server Full mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server is full, skipping!\n"); goto err_out; case 401: // Service Unavailable mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return service unavailable, skipping!\n"); goto err_out; case 403: // Service Forbidden mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server return 'Service Forbidden'\n"); goto err_out; case 404: // Resource Not Found mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: ICY-Server couldn't find requested stream, skipping!\n"); goto err_out; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Error: unhandled ICY-Errorcode, contact MPlayer developers!\n"); goto err_out; } } // Assume standard http if not ICY switch( http_hdr->status_code ) { case 200: // OK // Look if we can use the Content-Type content_type = http_get_field( http_hdr, "Content-Type" ); if( content_type!=NULL ) { char *content_length = NULL; mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", content_type ); if( (content_length = http_get_field(http_hdr, "Content-Length")) != NULL) mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length")); // Check in the mime type table for a demuxer type i = 0; while(mime_type_table[i].mime_type != NULL) { if( !strcasecmp( content_type, mime_type_table[i].mime_type ) ) { *file_format = mime_type_table[i].demuxer_type; res = seekable; goto out; } i++; } } // Not found in the mime type table, don't fail, // we should try raw HTTP res = seekable; goto out; // Redirect case 301: // Permanently case 302: // Temporarily case 303: // See Other // TODO: RFC 2616, recommand to detect infinite redirection loops next_url = http_get_field( http_hdr, "Location" ); if( next_url!=NULL ) { stream->streaming_ctrl->url = url_redirect( &url, next_url ); redirect = 1; } break; case 401: // Authentication required if( http_authenticate(http_hdr, url, &auth_retry)<0 ) goto err_out; redirect = 1; break; default: mp_msg(MSGT_NETWORK,MSGL_ERR,"Server returned %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); goto err_out; } } while( redirect );err_out: if (fd > 0) closesocket( fd ); fd = -1; res = STREAM_UNSUPPORTED; http_free( http_hdr ); http_hdr = NULL;out: stream->streaming_ctrl->data = (void*)http_hdr; stream->fd = fd; return res;}static int fixup_open(stream_t *stream,int seekable) { HTTP_header_t *http_hdr = stream->streaming_ctrl->data; int is_icy = http_hdr && http_get_field(http_hdr, "Icy-MetaInt"); int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0; stream->type = STREAMTYPE_STREAM; if(!is_icy && !is_ultravox && seekable) { stream->flags |= STREAM_SEEK; stream->seek = http_seek; } stream->streaming_ctrl->bandwidth = network_bandwidth; if ((!is_icy && !is_ultravox) || scast_streaming_start(stream)) if(nop_streaming_start( stream )) { mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_start failed\n"); if (stream->fd >= 0) closesocket(stream->fd); stream->fd = -1; streaming_ctrl_free(stream->streaming_ctrl); stream->streaming_ctrl = NULL; return STREAM_UNSUPPORTED; } fixup_network_stream_cache(stream); return STREAM_OK;}static int open_s1(stream_t *stream,int mode, void* opts, int* file_format) { int seekable=0; URL_t *url; stream->streaming_ctrl = streaming_ctrl_new(); if( stream->streaming_ctrl==NULL ) { return STREAM_ERROR; } stream->streaming_ctrl->bandwidth = network_bandwidth; url = url_new(stream->url); stream->streaming_ctrl->url = check4proxies(url); url_free(url); mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(1), URL: %s\n", stream->url); seekable = http_streaming_start(stream, file_format); if((seekable < 0) || (*file_format == DEMUXER_TYPE_ASF)) { if (stream->fd >= 0) closesocket(stream->fd); stream->fd = -1; streaming_ctrl_free(stream->streaming_ctrl); stream->streaming_ctrl = NULL; return STREAM_UNSUPPORTED; } return fixup_open(stream, seekable);}static int open_s2(stream_t *stream,int mode, void* opts, int* file_format) { int seekable=0; URL_t *url; stream->streaming_ctrl = streaming_ctrl_new(); if( stream->streaming_ctrl==NULL ) { return STREAM_ERROR; } stream->streaming_ctrl->bandwidth = network_bandwidth; url = url_new(stream->url); stream->streaming_ctrl->url = check4proxies(url); url_free(url); mp_msg(MSGT_OPEN, MSGL_V, "STREAM_HTTP(2), URL: %s\n", stream->url); seekable = http_streaming_start(stream, file_format); if(seekable < 0) { if (stream->fd >= 0) closesocket(stream->fd); stream->fd = -1; streaming_ctrl_free(stream->streaming_ctrl); stream->streaming_ctrl = NULL; return STREAM_UNSUPPORTED; } return fixup_open(stream, seekable);}stream_info_t stream_info_http1 = { "http streaming", "null", "Bertrand, Albeau, Reimar Doeffinger, Arpi?", "plain http", open_s1, {"http", "http_proxy", "unsv", NULL}, NULL, 0 // Urls are an option string};stream_info_t stream_info_http2 = { "http streaming", "null", "Bertrand, Albeu, Arpi? who?", "plain http, also used as fallback for many other protocols", open_s2, {"http", "http_proxy", "pnm", "mms", "mmsu", "mmst", "rtsp", NULL}, //all the others as fallback NULL, 0 // Urls are an option string};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -