HTTP_header_t *  http_read_response_with_content(int fd) {
	HTTP_header_t *http_hdr;
	char response[BUFFER_SIZE];
	int i;
	http_hdr = http_new_header();

	if (http_hdr == NULL) {
		return NULL;
	}

	do {
		i = recv(fd, response, BUFFER_SIZE, 0);

		if (i < 0) {
			mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ReadFailed);
			break;
		}

		if (i == 0) {
			mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
			break;
		}

		http_response_append(http_hdr, response, i);
	} while (1);

	if ((http_hdr->buffer_size == 0) || http_response_parse(http_hdr) < 0) {
		http_free(http_hdr);
		return NULL;
	}

	return http_hdr;
}
Exemple #2
0
HTTP_header_t *
http_read_response( int fd ) {
	HTTP_header_t *http_hdr;
	char response[BUFFER_SIZE];
	int i;

	http_hdr = http_new_header();
	if( http_hdr==NULL ) {
		return NULL;
	}

	do {
		i = recv( fd, response, BUFFER_SIZE, 0 );
		if( i<0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
			http_free( http_hdr );
			return NULL;
		}
		if( i==0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
			http_free( http_hdr );
			return NULL;
		}
		http_response_append( http_hdr, response, i );
	} while( !http_is_header_entire( http_hdr ) );
	http_response_parse( http_hdr );
	return http_hdr;
}
Exemple #3
0
HTTP_header_t *
http_read_response( int fd ) {
    HTTP_header_t *http_hdr;
    char response[BUFFER_SIZE];
    int i;

    http_hdr = http_new_header();
    if( http_hdr==NULL ) {
        return NULL;
    }

    do {
        i = recv( fd, response, BUFFER_SIZE, 0 );
        if( i<0 ) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
            http_free( http_hdr );
            return NULL;
        }
        if( i==0 ) {
            mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
            http_free( http_hdr );
            return NULL;
        }
        http_response_append( http_hdr, response, i );
    } while( !http_is_header_entire( http_hdr ) );
    if (http_response_parse( http_hdr ) < 0) {
        http_free( http_hdr );
        return NULL;
    }
    return http_hdr;
}
Exemple #4
0
/* Send req and get response */
int http_transaction(http_t *client, http_trans_t *trans)
{
	int rc = 0;

	ASSERT(client);
	ASSERT(trans);

	if (!client->initialized)
		return RC_HTTP_OBJECT_NOT_INITIALIZED;

	trans->rsp_len = 0;
	do {
#ifdef ENABLE_SSL
		if (client->ssl_enabled) {
			TRY(ssl_send(client, trans->p_req, trans->req_len));
			TRY(ssl_recv(client, trans->p_rsp, trans->max_rsp_len, &trans->rsp_len));
		}
		else
#endif
		{
			TRY(tcp_send(&client->tcp, trans->p_req, trans->req_len));
			TRY(tcp_recv(&client->tcp, trans->p_rsp, trans->max_rsp_len, &trans->rsp_len));
		}
	}
	while (0);

	trans->p_rsp[trans->rsp_len] = 0;
	http_response_parse(trans);

	return rc;
}
Exemple #5
0
static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
	char *content_type, *pragma;
	char features[64] = "\0";
	size_t len;
	if( http_response_parse(http_hdr)<0 ) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
		return -1;
	}
	switch( http_hdr->status_code ) {
		case 200:
			break;
		case 401: // Authentication required
			return ASF_Authenticate_e;
		default:
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
			return -1;
	}

	content_type = http_get_field( http_hdr, "Content-Type");
//printf("Content-Type: [%s]\n", content_type);

	pragma = http_get_field( http_hdr, "Pragma");
	while( pragma!=NULL ) {
		char *comma_ptr=NULL;
		char *end;
//printf("Pragma: [%s]\n", pragma );
		// The pragma line can get severals attributes
		// separeted with a comma ','.
		do {
			if( !strncasecmp( pragma, "features=", 9) ) {
				pragma += 9;
				end = strstr( pragma, "," );
				if( end==NULL ) {
				  len = strlen(pragma);
				} else {
				  len = (unsigned int)(end-pragma);
				}
				if(len > sizeof(features) - 1) {
				  mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
				  len = sizeof(features) - 1;
				}
				strncpy( features, pragma, len );
				features[len]='\0';
				break;
			}
			comma_ptr = strstr( pragma, "," );
			if( comma_ptr!=NULL ) {
				pragma = comma_ptr+1;
				if( pragma[0]==' ' ) pragma++;
			}
		} while( comma_ptr!=NULL );
		pragma = http_get_next_field( http_hdr );
	}
	asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
	return 0;
}
HTTP_header_t *  http_read_response(int fd) {

	HTTP_header_t *http_hdr = NULL;
	char response[BUFFER_SIZE];
	int i;
	http_hdr = http_new_header();

	if (http_hdr == NULL) {
		return NULL;
	}

	memset(response,0,BUFFER_SIZE);
	
	do {

	 if(is_file_seq_exit())
	   {
         //yliu add:for exit 
         http_free( http_hdr );
        
	     OS_PRINTF("\n%s %d stream check :%d\n",__func__,__LINE__,i);
	     return NULL;
	   }

		i = recv( fd, response, BUFFER_SIZE, 0 );
		if( i<0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
			http_free( http_hdr );
			return NULL;
		}
		
		if( i==0 ) {
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
			http_free( http_hdr );
			return NULL;
		}
		
		http_response_append( http_hdr, response, i );
		
	} while( !http_is_header_entire( http_hdr ) );

	
	if (http_response_parse( http_hdr ) < 0) {
		http_free( http_hdr );
		return NULL;
	}
	
	return http_hdr;
	
}
Exemple #7
0
/* Receives an HTTP response from socket SOCKFD; returns a decoded KVMessage.
 * Returns NULL if there is an error. */
kvresponse_t *kvresponse_recieve(int sockfd) {
  kvresponse_t *kvres = calloc(1, sizeof(kvresponse_t));

  struct http_response *res = http_response_parse(sockfd);
  if (!res) goto error;
  kvres->type = kvresponse_get_status_code(res->status);
  if (kvres->type == EMPTY) goto error;
  kvres->body = res->body;

  free(res);
  return kvres;

error:
  http_response_free(res);
  kvresponse_free(kvres);
  return NULL;
}
Exemple #8
0
/* Send req and get response */
int http_transaction(http_t *client, http_trans_t *trans)
{
	int rc = 0;

	ASSERT(client);
	ASSERT(trans);

	if (!client->initialized)
		return RC_HTTP_OBJECT_NOT_INITIALIZED;

	trans->rsp_len = 0;
	do {
		TRY(ssl_send(client, trans->req, trans->req_len));
		TRY(ssl_recv(client, trans->rsp, trans->max_rsp_len, &trans->rsp_len));
	}
	while (0);

	trans->rsp[trans->rsp_len] = 0;
	http_response_parse(trans);

	return rc;
}
Exemple #9
0
int hispider_packet_handler(CONN *conn, CB_DATA *packet)
{
    char *p = NULL, *end = NULL, *ip = NULL, *host = NULL, *path = NULL;
    HTTP_RESPONSE http_resp = {0};
    int taskid = 0, n = 0;
    int c_id = 0, port = 0;
    struct hostent *hp = NULL;

    if(conn && tasklist && (c_id = conn->c_id) >= 0 && c_id < ntask)
    {
        p = packet->data;
        end = packet->data + packet->ndata;
        /* http handler */
        if(conn == tasklist[c_id].c_conn)
        {
            if(p == NULL || http_response_parse(p, end, &http_resp) == -1)
            {
                conn->over_cstate(conn);
                conn->over(conn);
                return http_download_error(c_id);
            }
            if(http_resp.respid == RESP_OK && http_resp.headers[HEAD_ENT_CONTENT_TYPE]
                    && strncasecmp(http_resp.headers[HEAD_ENT_CONTENT_TYPE], "text", 4) == 0)
            {
                conn->save_cache(conn, &http_resp, sizeof(HTTP_RESPONSE));
                if((p = http_resp.headers[HEAD_ENT_CONTENT_LENGTH]) && (n = atol(p)) > 0)
                {
                    conn->recv_chunk(conn, n);
                }
                else
                {
                    conn->set_timeout(conn, HTTP_DOWNLOAD_TIMEOUT);
                    conn->recv_chunk(conn, HTML_MAX_SIZE);
                }
            }
            else
            {
                conn->over_cstate(conn);
                conn->close(conn);
                return http_download_error(c_id);
            }
        }
        /* task handler */
        else if(conn == tasklist[c_id].s_conn)
        {
            if(p == NULL || http_response_parse(p, end, &http_resp) == -1)
            {
                conn->over_cstate(conn);
                conn->over(conn);
                QUEUE_PUSH(taskqueue, int, &c_id);
                tasklist[c_id].s_conn = NULL;
                return -1;
            }
            if(http_resp.respid == RESP_OK)
            {
                host = http_resp.headers[HEAD_REQ_HOST];
                ip = http_resp.headers[HEAD_RESP_SERVER];
                port = (http_resp.headers[HEAD_REQ_REFERER])
                       ? atoi(http_resp.headers[HEAD_REQ_REFERER]) : 0;
                path = http_resp.headers[HEAD_RESP_LOCATION];
                taskid = tasklist[c_id].taskid = (http_resp.headers[HEAD_REQ_FROM])
                                                 ? atoi(http_resp.headers[HEAD_REQ_FROM]) : 0;
                //fprintf(stdout, "%s::%d OK host:%s ip:%s port:%d path:%s taskid:%d \n",
                //        __FILE__, __LINE__, host, ip, port, path, taskid);
                if(host == NULL || ip == NULL || path == NULL || port == 0 || taskid < 0)
                    goto restart_task;
                if(strcmp(ip, "255.255.255.255") == 0)
                {
                    if((hp = gethostbyname(host)))
                    {
                        tasklist[c_id].is_new_host = 1;
                        strcpy(tasklist[c_id].host, host);
                        sprintf(tasklist[c_id].ip, "%s",
                                inet_ntoa(*((struct in_addr *)(hp->h_addr))));
                        ip = tasklist[c_id].ip;
                    }
                    else
                    {
                        DEBUG_LOGGER(logger, "Resolving name[%s] failed, %s",
                                     host, strerror(h_errno));
                        goto restart_task;
                    }
                }
                if((tasklist[c_id].c_conn = service->newconn(service, -1, -1, ip, port, NULL)))
                {
                    tasklist[c_id].nrequest = sprintf(tasklist[c_id].request,
                                                      "GET %s HTTP/1.0\r\nHost: %s\r\n"
                                                      "User-Agent: %s\r\nAccept: %s\r\nAccept-Language: %s\r\n"
                                                      "Accept-Encoding: %s\r\nAccept-Charset: %s\r\nConnection: close\r\n\r\n",
                                                      //"Accept-Charset: %s\r\nConnection: close\r\n\r\n",
                                                      path, host, USER_AGENT, ACCEPT_TYPE, ACCEPT_LANGUAGE,
                                                      ACCEPT_ENCODING, ACCEPT_CHARSET);
                    tasklist[c_id].c_conn->c_id = c_id;
                    tasklist[c_id].c_conn->start_cstate(tasklist[c_id].c_conn);
                    return service->newtransaction(service, tasklist[c_id].c_conn, c_id);
                }
                else
                {
                    ERROR_LOGGER(logger, "Connect to [%s][%s:%d] failed, %s\n",
                                 host, ip, port, strerror(errno));
                }
            }
restart_task:
            return http_download_error(c_id);
        }