示例#1
0
文件: main.c 项目: jucs/musicfs
static void fuse_destroyed() {	
	logging_log("Main", LOGGING_LEVEL_INFO,
			"*** Got EXIT COMMAND from FUSE, termination sequence initiated...");
	
	logging_log("Main", LOGGING_LEVEL_INFO,
			"Terminating all other threads...");
	
	downloader_shutdown();
	
	logging_log("Main", LOGGING_LEVEL_INFO,
			"All other threads have terminated, cleaning up...");
	
	http_free();
	downloader_free();
	searcher_free();
	filesystem_free();
	providers_free();
	configuration_free();
	
	logging_log("Main", LOGGING_LEVEL_INFO,
			"Cleanup almost finished, cleaning up the logging subsystem and exiting...");
	logging_free();
	
	fclose(stdin);
	fclose(stdout);
	fclose(stderr);
}
示例#2
0
文件: dlight.c 项目: fiktivkod/dlight
static void process(struct cconf *config) {

	int i;
	struct buffer *data;

	proc_cache_purge(PROC_CACHE_PURGE_INTERVAL);
	dlhist_purge(DLHIST_PURGE_INTERVAL);

	for(i=0; i < config->nr; i++) {
		struct target *t = config->target + i;
		rss_t rss;

		data = http_fetch_page(t->src);
		if (!data)
			continue;

		rss = rss_parse(data->block, data->len);
		if (!rss) {
			error("failed to parse rss: %s", t->src);
			continue;
		}

		process_items(rss, t);
		rss_free(rss);
		http_free(data);
	}
}
示例#3
0
文件: cfg.c 项目: lubing521/libhttp
void
http_cfg_free(struct http_cfg *cfg) {
    http_free(cfg->content_decoders);
    http_headers_delete(cfg->default_headers);

    memset(cfg, 0, sizeof(struct http_cfg));
}
示例#4
0
文件: http.c 项目: zerix/mpv
static int scast_streaming_start(stream_t *stream) {
  int metaint;
  scast_data_t *scast_data;
  HTTP_header_t *http_hdr = stream->streaming_ctrl->data;
  if (!stream || stream->fd < 0 || !http_hdr)
    return -1;
  int is_ultravox = strcasecmp(stream->streaming_ctrl->url->protocol, "unsv") == 0;
  if (is_ultravox)
    metaint = 0;
  else {
    metaint = -1;
    char *h = http_get_field(http_hdr, "Icy-MetaInt");
    if (h)
        metaint = atoi(h);
    if (metaint <= 0)
      return -1;
  }
  stream->streaming_ctrl->buffer = malloc(http_hdr->body_size);
  stream->streaming_ctrl->buffer_size = http_hdr->body_size;
  stream->streaming_ctrl->buffer_pos = 0;
  memcpy(stream->streaming_ctrl->buffer, http_hdr->body, http_hdr->body_size);
  scast_data = malloc(sizeof(scast_data_t));
  scast_data->metaint = metaint;
  scast_data->metapos = 0;
  scast_data->is_ultravox = is_ultravox;
  http_free(http_hdr);
  stream->streaming_ctrl->data = scast_data;
  stream->streaming_ctrl->streaming_read = scast_streaming_read;
  stream->streaming_ctrl->streaming_seek = NULL;
  stream->streaming_ctrl->status = streaming_playing_e;
  stream->streaming = true;
  return 0;
}
示例#5
0
/* Free a flow_t object */
int 
flow_free(flow_t *f)
{
	packet_t *cp;
	http_pair_t	*h;
	while(f->pkt_dst_f != NULL){
		cp = f->pkt_dst_f;
		f->pkt_dst_f = f->pkt_dst_f->next;
		packet_free(cp);
	}

	while(f->pkt_src_f != NULL){
		cp = f->pkt_src_f;
		f->pkt_src_f = f->pkt_src_f->next;
		packet_free(cp);
	}

	if(f->order != NULL){
		order_free(f->order);
	}

	if(f->http_f != NULL){
		h = f->http_f;
		f->http_f = f->http_f->next;
		http_free(h);
	}

	free(f);
	return 0;
}
示例#6
0
static int http_on_read(void *client_data, int idx, char *data, int len)
{
	proxy_info_t *info = client_data;

	if (!strncasecmp("HTTP/1.0 200", data, 12)) {
		/* Yay, we're approved. */
		info->status = 1;
		return(0);
	}

	if (len != 0) {
		/* Just some header that we don't care about. */
		return(0);
	}

	/* It's the blank line, so the http response is done. */

	if (info->status != 1) {
		/* Boo, we weren't approved. */
		http_err(info, ECONNREFUSED, "HTTP server refused to relay connection");
		return(0);
	}

	/* Successful, switch it over. */
	sockbuf_set_sock(info->our_idx, -1, 0);
	sockbuf_set_sock(info->their_idx, info->sock, 0);
	sockbuf_on_connect(info->their_idx, SOCKBUF_LEVEL_INTERNAL, info->host, info->port);
	http_free(info);

	return(0);
}
示例#7
0
/*
 * If resolved successfuly it will connect. Otherwise invokes
 * user callback as cb(undefined, error_message)
 */
static void http_get_dns_cb(const char *name, ip_addr_t *ipaddr, void *arg) {
  /* WIP: for now return the dns address as if it were the `get` response */
  struct espconn *conn = (struct espconn *) arg;
  struct http_ctx *ctx = (struct http_ctx *) conn->proto.tcp;
  static char err_msg[] = "cannot resolve";

  if (ipaddr == NULL) {
    v7_val_t res, cb_args = v7_create_object(v7);
    v7_own(v7, &cb_args);
    v7_array_set(v7, cb_args, 0, ctx->cb);
    v7_array_set(v7, cb_args, 1,
                 v7_create_string(v7, err_msg, sizeof(err_msg), 1));
    http_free(conn);
    if (v7_exec_with(v7, &res, "this[0](undefined, this[1])", cb_args) !=
        V7_OK) {
      v7_fprintln(stderr, v7, res);
    }
    v7_disown(v7, &cb_args);
    v7_disown(v7, &ctx->body); /* body has not been sent yet */
    v7_disown(v7, &ctx->cb);
  } else {
    memcpy(conn->proto.tcp->remote_ip, &ipaddr->addr, 4);
    conn->proto.tcp->remote_port = ctx->port;
    conn->proto.tcp->local_port = espconn_port();

    espconn_regist_connectcb(conn, http_connect_cb);
    espconn_regist_disconcb(conn, http_disconnect_cb);
    espconn_regist_reconcb(conn, http_error_cb);
    espconn_connect(conn);
  }
}
示例#8
0
/* Invoke user callback as cb(data, undefined) */
static void http_disconnect_cb(void *arg) {
  struct espconn *conn = (struct espconn *) arg;
  struct http_ctx *ctx = (struct http_ctx *) conn->proto.tcp;
  v7_val_t data, cb_args;
  char *body;
  int i;
  v7_val_t res;

  body = ctx->resp;
  for (i = 0; i + 3 < ctx->resp_pos; i++) {
    if (memcmp(ctx->resp + i, "\r\n\r\n", 4) == 0) {
      body = ctx->resp + i + 4;
      break;
    }
  }

  cb_args = v7_create_object(v7);
  v7_own(v7, &cb_args);

  data = v7_create_string(v7, body, ctx->resp_pos - (body - ctx->resp), 1);
  v7_own(v7, &data);
  http_free(conn);

  v7_array_set(v7, cb_args, 0, ctx->cb);
  v7_array_set(v7, cb_args, 1, data);
  v7_disown(v7, &data);

  if (v7_exec_with(v7, &res, "this[0](this[1])", cb_args) != V7_OK) {
    v7_fprintln(stderr, v7, res);
  }
  v7_disown(v7, &cb_args);
  v7_disown(v7, &ctx->cb);
}
示例#9
0
/* This one is actually pretty simple... Might get more calls if we can't write 
   the whole request at once. */
static gboolean http_connected( gpointer data, int source, b_input_condition cond )
{
	struct http_request *req = data;
	int st;
	
	if( source < 0 )
		goto error;
	
	if( req->inpa > 0 )
		b_event_remove( req->inpa );
	
	sock_make_nonblocking( req->fd );
	
	if( req->ssl )
	{
		st = ssl_write( req->ssl, req->request + req->bytes_written,
		                req->request_length - req->bytes_written );
		if( st < 0 )
		{
			if( ssl_errno != SSL_AGAIN )
			{
				ssl_disconnect( req->ssl );
				goto error;
			}
		}
	}
	else
	{
		st = write( source, req->request + req->bytes_written,
		                    req->request_length - req->bytes_written );
		if( st < 0 )
		{
			if( !sockerr_again() )
			{
				closesocket( req->fd );
				goto error;
			}
		}
	}
	
	if( st > 0 )
		req->bytes_written += st;
	
	if( req->bytes_written < req->request_length )
		req->inpa = b_input_add( source,
		                         req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_WRITE,
	        	                 http_connected, req );
	else
		req->inpa = b_input_add( source, B_EV_IO_READ, http_incoming_data, req );
	
	return FALSE;
	
error:
	if( req->status_string == NULL )
		req->status_string = g_strdup( "Error while writing HTTP request" );
	
	req->func( req );
	http_free( req );
	return FALSE;
}
示例#10
0
int get_initial_cursor(const struct electorate *elec)
{
  struct http_vars *reply;
  int initial_cursor;
  /* SIPL 2011-09-26 Added one to length of array.  There was
     no chance of a buffer overflow, but do this to make it consistent
     with all other uses of INT_CHARS. */
  char ecodestr[INT_CHARS+1];
  struct http_vars request[]
    = { { (char*)"ecode", ecodestr }, { NULL, NULL } };

  sprintf(ecodestr, "%u", elec->code);

  reply = http_exchange(SERVER_ADDRESS, SERVER_PORT, CURSOR_CGI,request);
  if (!reply)
    display_error(ERR_SERVER_UNREACHABLE);

  initial_cursor = atoi(http_string(reply, "cursor"));

  http_free(reply);

  cursor = initial_cursor;

  return initial_cursor;
}
示例#11
0
int
http_seek( stream_t *stream, off_t pos ) {
	HTTP_header_t *http_hdr = NULL;
	int fd;
	if( stream==NULL ) return 0;

	if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
	fd = http_send_request( stream->streaming_ctrl->url, pos );
	if( fd<0 ) return 0;

	http_hdr = http_read_response( fd );

	if( http_hdr==NULL ) return 0;

	if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
		http_debug_hdr( http_hdr );

	switch( http_hdr->status_code ) {
		case 200:
		case 206: // OK
			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
			mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
			if( http_hdr->body_size>0 ) {
				if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
					http_free( http_hdr );
					return -1;
				}
			}
			break;
		default:
			mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
			closesocket( fd );
			fd = -1;
	}
	stream->fd = fd;

	if( http_hdr ) {
		http_free( http_hdr );
		stream->streaming_ctrl->data = NULL;
	}

	stream->pos=pos;

	return 1;
}
示例#12
0
文件: cw.c 项目: simonz05/ludis
static int 
cw_get(const char *url) 
{
    int rv;
    struct http_response *r;

    r = http_init();
    rv = http_get(r, url); 

    if (rv != LUDIS_OK)
        goto error;

    http_free(r);
    return LUDIS_OK;
error:
    http_free(r);
    return LUDIS_ERR;
}
示例#13
0
static int cddb_http_request(char *command,
                      int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
                      cddb_data_t *cddb_data)
{
    char request[4096];
    int fd, ret = 0;
    URL_t *url;
    HTTP_header_t *http_hdr;

    if (reply_parser == NULL || command == NULL || cddb_data == NULL)
        return -1;

    sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
            cddb_data->freedb_server, command, cddb_data->cddb_hello,
            cddb_data->freedb_proto_level);
    mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);

    url = url_new(request);
    if (url == NULL) {
        mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NotAValidURL);
        return -1;
    }

    fd = http_send_request(url,0);
    if (fd < 0) {
        mp_msg(MSGT_DEMUX, MSGL_ERR,
               MSGTR_MPDEMUX_CDDB_FailedToSendHTTPRequest);
        return -1;
    }

    http_hdr = http_read_response(fd);
    if (http_hdr == NULL) {
        mp_msg(MSGT_DEMUX, MSGL_ERR,
               MSGTR_MPDEMUX_CDDB_FailedToReadHTTPResponse);
        return -1;
    }

    http_debug_hdr(http_hdr);
    mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);

    switch (http_hdr->status_code) {
    case 200:
        ret = reply_parser(http_hdr, cddb_data);
        break;
    case 400:
        mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorNOTFOUND);
        break;
    default:
        mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorUnknown);
    }

    http_free(http_hdr);
    url_free(url);

    return ret;
}
示例#14
0
void
http_route_components_free(struct http_route_component *components,
                           size_t nb_components) {
    if (!components)
        return;

    for (size_t i = 0; i < nb_components; i++) {
        struct http_route_component *component;

        component = components + i;

        if (component->type == HTTP_ROUTE_COMPONENT_STRING
         || component->type == HTTP_ROUTE_COMPONENT_NAMED) {
            http_free(component->value);
        }
    }

    http_free(components);
}
示例#15
0
static int cddb_http_request(char *command,
                      int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
                      cddb_data_t *cddb_data)
{
    char request[4096];
    int fd, ret = 0;
    URL_t *url;
    HTTP_header_t *http_hdr;

    if (reply_parser == NULL || command == NULL || cddb_data == NULL)
        return -1;

    sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
            cddb_data->freedb_server, command, cddb_data->cddb_hello,
            cddb_data->freedb_proto_level);
    mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);

    url = url_new(request);
    if (url == NULL) {
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
        return -1;
    }

    fd = http_send_request(url,0);
    if (fd < 0) {
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
        return -1;
    }

    http_hdr = http_read_response(fd);
    if (http_hdr == NULL) {
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
        return -1;
    }

    http_debug_hdr(http_hdr);
    mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);

    switch (http_hdr->status_code) {
    case 200:
        ret = reply_parser(http_hdr, cddb_data);
        break;
    case 400:
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
        break;
    default:
        mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
    }

    http_free(http_hdr);
    url_free(url);

    return ret;
}
示例#16
0
int main()
{
	struct http_server *httpd;
	struct timeval timeout = { 2, 0 };	
#ifdef WIN32
	{
		WSADATA wd;
		WSAStartup( MAKEWORD( 2, 0 ), &wd );
	}
#endif
	
#ifdef WIN32
	if( !SetConsoleCtrlHandler( Handler, TRUE ) )
	{
		fprintf( stderr, "error setting event handler.\n" );
		return -1;
	}
#endif

	http_set_info_log( info_log );
	httpd = http_start( "0.0.0.0", 8080, 1024 );
	http_set_rcb( httpd, handle_request, 0 );

#ifdef CGI_SUPPORT
#ifdef CGI_LUA_SUPPORT
	luaC_init( httpd );
	luaC_set_script_cb( file_exist, load_file );
#else
	http_set_cgi_cb( httpd, handle_cgi_query, 0 );
#endif
#endif

	printf( "server startup\n" );
	
	while( is_done == 0 )
	{
		http_poll( httpd, &timeout );
	}

#ifdef CGI_LUA_SUPPORT
	luaC_release();
#endif

	http_free( httpd );
	
#ifdef WIN32
	WSACleanup();
#endif
	printf( "server shutdown\n" );
	return 0;
}
示例#17
0
文件: uri.c 项目: lubing521/libhttp
char *
http_uri_decode_query_component(const char *str, size_t sz) {
    const char *iptr;
    char *component, *optr;
    size_t str_length, ilen;

    str_length = strlen(str);
    component = http_malloc(str_length + 1);

    iptr = str;
    ilen = sz;

    optr = component;

    while (ilen > 0) {
        if (*iptr == '%') {
            int d1, d2;

            if (ilen < 3) {
                http_set_error("truncated escape sequence");
                goto error;
            }

            if (http_read_hex_digit((unsigned char)iptr[1], &d1) == -1
             || http_read_hex_digit((unsigned char)iptr[2], &d2) == -1) {
                http_set_error("invalid escape sequence");
                goto error;
            }

            *optr++ = (d1 << 4) | d2;
            iptr += 3;
            ilen -= 3;
        } else if (*iptr == '+') {
            *optr++ = ' ';
            iptr++;
            ilen--;
        } else {
            *optr++ = *iptr++;
            ilen--;
        }
    }

    *optr = '\0';
    return component;

error:
    http_free(component);
    return NULL;
}
示例#18
0
文件: uri.c 项目: lubing521/libhttp
void
http_uri_delete(struct http_uri *uri) {
    if (!uri)
        return;

    http_free(uri->scheme);
    http_free(uri->user);
    http_free(uri->password);
    http_free(uri->host);
    http_free(uri->port);
    http_free(uri->path);
    http_free(uri->fragment);

    for (size_t i = 0; i < uri->nb_query_parameters; i++)
        http_query_parameter_free(uri->query_parameters + i);
    http_free(uri->query_parameters);

    memset(uri, 0, sizeof(struct http_uri));
    http_free(uri);
}
示例#19
0
文件: bench.c 项目: ifzz/libhttp
static void parse_response( void )
{
    char res[] =
        "HTTP/1.1 200 OK\r\n"
        "Server: GitHub.com\r\n"
        "Date: Sat, 27 Jun 2015 04:10:06 GMT\r\n"
        "Content-Type: text/html; charset=utf-8\r\n"
        "Transfer-Encoding: chunked\r\n"
        "Status: 200 OK\r\n"
        "Content-Security-Policy: default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src 'self' 'unsafe-inline' 'unsafe-eval' assets-cdn.github.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src 'none'; frame-src 'self' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src 'self' live.github.com wss://live.github.com uploads.github.com status.github.com api.github.com www.google-analytics.com github-cloud.s3.amazonaws.com\r\n"
        "Cache-Control: no-cache\r\n"
        "Vary: X-PJAX\r\n"
        "X-UA-Compatible: IE=Edge,chrome=1\r\n"
        "Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Wed, 27 Jun 2035 04:10:06 -0000; secure; HttpOnly\r\n"
        "X-Request-Id: 8ea8d359ded1d2d30094d38b2a4e73d3\r\n"
        "X-Runtime: 0.037659\r\n"
        "X-GitHub-Request-Id: 999DDDE5:0A3A:102882A:558E221D\r\n"
        "Strict-Transport-Security: max-age=31536000; includeSubdomains; preload\r\n"
        "X-Content-Type-Options: nosniff\r\n"
        "X-XSS-Protection: 1; mode=block\r\n"
        "X-Frame-Options: deny\r\n"
        "Vary: Accept-Encoding\r\n"
        "X-Served-By: 1868c9f28a71d80b2987f48dbd1824a0\r\n"
        "\r\n";
    size_t len = sizeof( res );
    uint64_t i = 0;
    int rc = 0;
    float start = 0, end = 0, elapsed = 0;
    uint16_t maxhdrlen = UINT16_MAX;
    http_t *r = http_alloc(20);
    
    start = (float)clock()/CLOCKS_PER_SEC;
    for( i = 0; i < NLOOP; i++ ){
        rc = http_res_parse( r, res, len, maxhdrlen );
        assert( rc == HTTP_SUCCESS );
        assert( http_version(r) == HTTP_V11 );
        assert( http_status(r) == HTTP_OK );
        assert( r->nheader == 19 );
        http_init( r );
    }
    end = (float)clock()/CLOCKS_PER_SEC;
    elapsed = end - start;
    
    http_free( r );
    
    printf("\tElapsed %f seconds.\n", elapsed );
    printf("\t%0.9f -> %f req/sec.\n", elapsed / NLOOP, 1.00000 / ( elapsed / NLOOP ) );
}
示例#20
0
文件: uri.c 项目: lubing521/libhttp
void
http_uri_encode_query_component(const char *str, struct bf_buffer *buf) {
    static const char *hex_digits = "0123456789abcdef";

    const char *iptr;
    char *optr;
    char *tmp;
    size_t len;

    /* Compute the size of the encoded string */
    len = 0;
    iptr = str;
    while (*iptr != '\0') {
        if (http_uri_is_query_component_char((unsigned char)*iptr)) {
            len++;
        } else {
            len += 3; /* '%xx' */
        }

        iptr++;
    }

    /* Encode the string */
    tmp = http_malloc(len);

    iptr = str;
    optr = tmp;
    while (*iptr != '\0') {
        if (http_uri_is_query_component_char((unsigned char)*iptr)) {
            *optr++ = *iptr;
        } else if (*iptr == ' ') {
            *optr++ = '+';
        } else {
            unsigned char c;

            c = (unsigned char)*iptr;

            *optr++ = '%';
            *optr++ = hex_digits[c >> 4];
            *optr++ = hex_digits[c & 0xf];
        }

        iptr++;
    }

    bf_buffer_add(buf, tmp, len);
    http_free(tmp);
}
示例#21
0
/* Invoke user callback as cb(undefined, err_msg) */
static void http_error_cb(void *arg, int8_t err) {
  struct espconn *conn = (struct espconn *) arg;
  struct http_ctx *ctx = (struct http_ctx *) conn->proto.tcp;
  char err_msg[128];
  v7_val_t res, cb_args;

  cb_args = v7_create_object(v7);
  v7_own(v7, &cb_args);

  snprintf(err_msg, sizeof(err_msg), "connection error: %d\n", err);
  v7_array_set(v7, cb_args, 0, ctx->cb);
  v7_array_set(v7, cb_args, 1,
               v7_create_string(v7, err_msg, sizeof(err_msg), 1));
  http_free(conn);
  if (v7_exec_with(v7, &res, "this[0](undefined, this[1])", cb_args) != V7_OK) {
    v7_fprintln(stderr, v7, res);
  }
  v7_disown(v7, &cb_args);
}
示例#22
0
文件: bench.c 项目: ifzz/libhttp
static void parse_request( void )
{
    char req[] = 
        "GET /mah0x211/libhttp HTTP/1.1\r\n"
        "Host: github.com\r\n"
        "Connection: keep-alive\r\n"
        "Keep-Alive: 115\r\n"
        "Cache-Control: max-age=0\r\n"
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n"
        "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36\r\n"
        "Accept-Encoding: gzip, deflate, sdch\r\n"
        "Accept-Language: ja,en-US;q=0.8,en;q=0.6\r\n"
        "Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n"
        "Cookie: __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; _octo=XXX.X.XXXXXXXX.XXXXXXXXXX; logged_in=XX; _ga=XXX.X.XXXXXXXXX.XXXXXXXXXX\r\n"
        "\r\n";
    size_t len = sizeof( req ) + 1;
    uint64_t i = 0;
    int rc = 0;
    float start = 0, end = 0, elapsed = 0;
    uint16_t maxurilen = UINT16_MAX;
    uint16_t maxhdrlen = UINT16_MAX;
    uint8_t nheader = 20;
    http_t *r = http_alloc( nheader );
    
    start = (float)clock()/CLOCKS_PER_SEC;
    for( i = 0; i < NLOOP; i++ ){
        rc = http_req_parse( r, req, len, maxurilen, maxhdrlen );
        assert( rc == HTTP_SUCCESS );
        assert( http_method(r) == HTTP_MGET );
        assert( http_version(r) == HTTP_V11 );
        assert( r->nheader == 10 );
        //keys( r, req );
        http_init( r );
    }
    end = (float)clock()/CLOCKS_PER_SEC;
    elapsed = end - start;
    
    http_free( r );
    
    printf("\tElapsed %f seconds.\n", elapsed );
    printf("\t%0.9f -> %f req/sec.\n", elapsed / NLOOP, 1.00000 / ( elapsed / NLOOP ) );
}
示例#23
0
struct http_request *http_dorequest( char *host, int port, int ssl, char *request, http_input_function func, gpointer data )
{
	struct http_request *req;
	int error = 0;
	
	req = g_new0( struct http_request, 1 );
	
	if( ssl )
	{
		req->ssl = ssl_connect( host, port, TRUE, http_ssl_connected, req );
		if( req->ssl == NULL )
			error = 1;
	}
	else
	{
		req->fd = proxy_connect( host, port, http_connected, req );
		if( req->fd < 0 )
			error = 1;
	}
	
	if( error )
	{
		http_free( req );
		return NULL;
	}
	
	req->func = func;
	req->data = data;
	req->request = g_strdup( request );
	req->request_length = strlen( request );
	req->redir_ttl = 3;
	req->content_length = -1;
	
	if( getenv( "BITLBEE_DEBUG" ) )
		printf( "About to send HTTP request:\n%s\n", req->request );
	
	return req;
}
示例#24
0
int
http_send_request( URL_t *url, off_t pos ) {
	HTTP_header_t *http_hdr;
	URL_t *server_url;
	char str[256];
	int fd = -1;
	int ret;
	int proxy = 0;		// Boolean

	http_hdr = http_new_header();

	if( !strcasecmp(url->protocol, "http_proxy") ) {
		proxy = 1;
		server_url = url_new( (url->file)+1 );
		http_set_uri( http_hdr, server_url->url );
	} else {
		server_url = url;
		http_set_uri( http_hdr, server_url->file );
	}
	if (server_url->port && server_url->port != 80)
	    snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
	else
	    snprintf(str, 256, "Host: %s", server_url->hostname );
	http_set_field( http_hdr, str);
	if (network_useragent)
	{
	    snprintf(str, 256, "User-Agent: %s", network_useragent);
	    http_set_field(http_hdr, str);
	}
	else
	    http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);

	if( strcasecmp(url->protocol, "noicyx") )
	    http_set_field(http_hdr, "Icy-MetaData: 1");

	if(pos>0) {
	// Extend http_send_request with possibility to do partial content retrieval
	    snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
	    http_set_field(http_hdr, str);
	}

	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );

	http_set_field( http_hdr, "Connection: close");
	http_add_basic_authentication( http_hdr, url->username, url->password );
	if( http_build_request( http_hdr )==NULL ) {
		goto err_out;
	}

	if( proxy ) {
		if( url->port==0 ) url->port = 8080;			// Default port for the proxy server
		fd = connect2Server( url->hostname, url->port,1 );
		url_free( server_url );
		server_url = NULL;
	} else {
		if( server_url->port==0 ) server_url->port = 80;	// Default port for the web server
		fd = connect2Server( server_url->hostname, server_url->port,1 );
	}
	if( fd<0 ) {
		goto err_out;
	}
	mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );

	ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
	if( ret!=(int)http_hdr->buffer_size ) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
		goto err_out;
	}

	http_free( http_hdr );

	return fd;
err_out:
	if (fd > 0) closesocket(fd);
	http_free(http_hdr);
	if (proxy && server_url)
		url_free(server_url);
	return -1;
}
示例#25
0
static int gc_lua( lua_State *L )
{
    http_free( ((lhttp_t*)lua_touserdata( L, 1 ))->r );
    return 0;
}
示例#26
0
static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond )
{
	struct http_request *req = data;
	int evil_server = 0;
	char buffer[2048];
	char *end1, *end2;
	int st;
	
	if( req->inpa > 0 )
		b_event_remove( req->inpa );
	
	if( req->ssl )
	{
		st = ssl_read( req->ssl, buffer, sizeof( buffer ) );
		if( st < 0 )
		{
			if( ssl_errno != SSL_AGAIN )
			{
				/* goto cleanup; */
				
				/* YAY! We have to deal with crappy Microsoft
				   servers that LOVE to send invalid TLS
				   packets that abort connections! \o/ */
				
				goto got_reply;
			}
		}
		else if( st == 0 )
		{
			goto got_reply;
		}
	}
	else
	{
		st = read( req->fd, buffer, sizeof( buffer ) );
		if( st < 0 )
		{
			if( !sockerr_again() )
			{
				req->status_string = g_strdup( strerror( errno ) );
				goto cleanup;
			}
		}
		else if( st == 0 )
		{
			goto got_reply;
		}
	}
	
	if( st > 0 )
	{
		req->reply_headers = g_realloc( req->reply_headers, req->bytes_read + st + 1 );
		memcpy( req->reply_headers + req->bytes_read, buffer, st );
		req->bytes_read += st;
	}
	
	/* There will be more! */
	req->inpa = b_input_add( req->fd,
	                         req->ssl ? ssl_getdirection( req->ssl ) : B_EV_IO_READ,
	                         http_incoming_data, req );
	
	if( ssl_pending( req->ssl ) )
		return http_incoming_data( data, source, cond );
	else
		return FALSE;

got_reply:
	/* Maybe if the webserver is overloaded, or when there's bad SSL
	   support... */
	if( req->bytes_read == 0 )
	{
		req->status_string = g_strdup( "Empty HTTP reply" );
		goto cleanup;
	}
	
	/* Zero termination is very convenient. */
	req->reply_headers[req->bytes_read] = 0;
	
	/* Find the separation between headers and body, and keep stupid
	   webservers in mind. */
	end1 = strstr( req->reply_headers, "\r\n\r\n" );
	end2 = strstr( req->reply_headers, "\n\n" );
	
	if( end2 && end2 < end1 )
	{
		end1 = end2 + 1;
		evil_server = 1;
	}
	else if( end1 )
	{
		end1 += 2;
	}
	else
	{
		req->status_string = g_strdup( "Malformed HTTP reply" );
		goto cleanup;
	}
	
	*end1 = 0;
	
	if( getenv( "BITLBEE_DEBUG" ) )
		printf( "HTTP response headers:\n%s\n", req->reply_headers );
	
	if( evil_server )
		req->reply_body = end1 + 1;
	else
		req->reply_body = end1 + 2;
	
	req->body_size = req->reply_headers + req->bytes_read - req->reply_body;
	
	if( ( end1 = strchr( req->reply_headers, ' ' ) ) != NULL )
	{
		if( sscanf( end1 + 1, "%d", &req->status_code ) != 1 )
		{
			req->status_string = g_strdup( "Can't parse status code" );
			req->status_code = -1;
		}
		else
		{
			char *eol;
			
			if( evil_server )
				eol = strchr( end1, '\n' );
			else
				eol = strchr( end1, '\r' );
			
			req->status_string = g_strndup( end1 + 1, eol - end1 - 1 );
			
			/* Just to be sure... */
			if( ( eol = strchr( req->status_string, '\r' ) ) )
				*eol = 0;
			if( ( eol = strchr( req->status_string, '\n' ) ) )
				*eol = 0;
		}
	}
	else
	{
		req->status_string = g_strdup( "Can't locate status code" );
		req->status_code = -1;
	}
	
	if( ( ( req->status_code >= 301 && req->status_code <= 303 ) ||
	      req->status_code == 307 ) && req->redir_ttl-- > 0 )
	{
		char *loc, *new_request, *new_host;
		int error = 0, new_port, new_proto;
		
		/* We might fill it again, so let's not leak any memory. */
		g_free( req->status_string );
		req->status_string = NULL;
		
		loc = strstr( req->reply_headers, "\nLocation: " );
		if( loc == NULL ) /* We can't handle this redirect... */
		{
			req->status_string = g_strdup( "Can't locate Location: header" );
			goto cleanup;
		}
		
		loc += 11;
		while( *loc == ' ' )
			loc ++;
		
		/* TODO/FIXME: Possibly have to handle relative redirections,
		   and rewrite Host: headers. Not necessary for now, it's
		   enough for passport authentication like this. */
		
		if( *loc == '/' )
		{
			/* Just a different pathname... */
			
			/* Since we don't cache the servername, and since we
			   don't need this yet anyway, I won't implement it. */
			
			req->status_string = g_strdup( "Can't handle recursive redirects" );
			
			goto cleanup;
		}
		else
		{
			/* A whole URL */
			url_t *url;
			char *s;
			const char *new_method;
			
			s = strstr( loc, "\r\n" );
			if( s == NULL )
				goto cleanup;
			
			url = g_new0( url_t, 1 );
			*s = 0;
			
			if( !url_set( url, loc ) )
			{
				req->status_string = g_strdup( "Malformed redirect URL" );
				g_free( url );
				goto cleanup;
			}
			
			/* Find all headers and, if necessary, the POST request contents.
			   Skip the old Host: header though. This crappy code here means
			   anything using this http_client MUST put the Host: header at
			   the top. */
			if( !( ( s = strstr( req->request, "\r\nHost: " ) ) &&
			       ( s = strstr( s + strlen( "\r\nHost: " ), "\r\n" ) ) ) )
			{
				req->status_string = g_strdup( "Error while rebuilding request string" );
				g_free( url );
				goto cleanup;
			}
			
			/* More or less HTTP/1.0 compliant, from my reading of RFC 2616.
			   Always perform a GET request unless we received a 301. 303 was
			   meant for this but it's HTTP/1.1-only and we're specifically
			   speaking HTTP/1.0. ...
			   
			   Well except someone at identi.ca's didn't bother reading any
			   RFCs and just return HTTP/1.1-specific status codes to HTTP/1.0
			   requests. Fuckers. So here we are, handle 301..303,307. */
			if( strncmp( req->request, "GET", 3 ) == 0 )
				/* GETs never become POSTs. */
				new_method = "GET";
			else if( req->status_code == 302 || req->status_code == 303 )
				/* 302 de-facto becomes GET, 303 as specified by RFC 2616#10.3.3 */
				new_method = "GET";
			else
				/* 301 de-facto should stay POST, 307 specifally RFC 2616#10.3.8 */
				new_method = "POST";
			
			/* Okay, this isn't fun! We have to rebuild the request... :-( */
			new_request = g_strdup_printf( "%s %s HTTP/1.0\r\nHost: %s%s",
			                               new_method, url->file, url->host, s );
			
			new_host = g_strdup( url->host );
			new_port = url->port;
			new_proto = url->proto;
			
			/* If we went from POST to GET, truncate the request content. */
			if( new_request[0] != req->request[0] && new_request[0] == 'G' &&
			    ( s = strstr( new_request, "\r\n\r\n" ) ) )
				s[4] = '\0';
			
			g_free( url );
		}
		
		if( req->ssl )
			ssl_disconnect( req->ssl );
		else
			closesocket( req->fd );
		
		req->fd = -1;
		req->ssl = NULL;
		
		if( getenv( "BITLBEE_DEBUG" ) )
			printf( "New headers for redirected HTTP request:\n%s\n", new_request );
	
		if( new_proto == PROTO_HTTPS )
		{
			req->ssl = ssl_connect( new_host, new_port, TRUE, http_ssl_connected, req );
			if( req->ssl == NULL )
				error = 1;
		}
		else
		{
			req->fd = proxy_connect( new_host, new_port, http_connected, req );
			if( req->fd < 0 )
				error = 1;
		}
		g_free( new_host );
		
		if( error )
		{
			req->status_string = g_strdup( "Connection problem during redirect" );
			g_free( new_request );
			goto cleanup;
		}
		
		g_free( req->request );
		g_free( req->reply_headers );
		req->request = new_request;
		req->request_length = strlen( new_request );
		req->bytes_read = req->bytes_written = req->inpa = 0;
		req->reply_headers = req->reply_body = NULL;
		
		return FALSE;
	}
	
	/* Assume that a closed connection means we're finished, this indeed
	   breaks with keep-alive connections and faulty connections. */
	req->finished = 1;

cleanup:
	if( req->ssl )
		ssl_disconnect( req->ssl );
	else
		closesocket( req->fd );
	
	if( getenv( "BITLBEE_DEBUG" ) && req )
		printf( "Finishing HTTP request with status: %s\n",
		        req->status_string ? req->status_string : "NULL" );
	
	req->func( req );
	http_free( req );
	return FALSE;
}
示例#27
0
/* DDS3.2.3: Authenticate */
int main(int argc, char *argv[])
{
	struct http_vars *vars;
	struct barcode_hash_entry *bcentry;
	struct barcode bc;
	char bchash[HASH_BITS+1];
	struct electorate *elecs, *i;
	PGconn *conn;
	int ppcode;

	/* Our own failure function */
	set_cgi_bailout();

	/* Can be called on slave as well as master */
	conn = connect_db_port("evacs", get_database_port());
	if (!conn) bailout("Could not open database connection\n");

	/* Copy barcode ascii code from POST arguments */
	vars = cgi_get_arguments();
	strncpy(bc.ascii, http_string(vars, "barcode"), sizeof(bc.ascii)-1);
	bc.ascii[sizeof(bc.ascii)-1] = '\0';
	http_free(vars);

	/* Extract data and checksum from ascii */
	if (!bar_decode_ascii(&bc))
		cgi_error_response(ERR_BARCODE_MISREAD);

	/* Hash the barcode to look up in the table */
	gen_hash(bchash, bc.data, sizeof(bc.data));

	bcentry = get_bhash_table(conn, bchash);
	if (!bcentry) {
		PQfinish(conn);
		fprintf(stderr, "Barcode `%s' not found\n", bc.ascii);
		cgi_error_response(ERR_BARCODE_AUTHENTICATION_FAILED);
	}

	/* DDS3.2.4: Check Unused */
	if (bcentry->used) {
		PQfinish(conn);
		fprintf(stderr, "Barcode `%s' already used\n", bc.ascii);
		cgi_error_response(ERR_BARCODE_USED);
	}

	ppcode = SQL_singleton_int(conn,"SELECT polling_place_code "
				   "FROM server_parameter;");
	if (ppcode < 0) {
		PQfinish(conn);
		cgi_error_response(ERR_SERVER_INTERNAL);
	}

	if (ppcode != bcentry->ppcode) {
		PQfinish(conn);
		cgi_error_response(ERR_BARCODE_PP_INCORRECT);
	}

	elecs = get_electorates(conn);
	for (i = elecs; i; i = i->next) {
		if (i->code == bcentry->ecode) {
			/* Found it! */
			vars = create_response(conn, i);
			free_electorates(elecs);
			PQfinish(conn);
			cgi_good_response(vars);
		}
	}

	/* Should never happen */
	free_electorates(elecs);
	PQfinish(conn);
	bailout("Barcode electorate %u not found\n", bcentry->ecode);
}
示例#28
0
文件: uri.c 项目: lubing521/libhttp
static int
http_uri_parse(const char *str, struct http_uri *uri) {
    const char *ptr, *start, *colon;
    size_t toklen;

    memset(uri, 0, sizeof(struct http_uri));

    ptr = str;

    if (*ptr == '\0') {
        http_set_error("empty string");
        return -1;
    }

    /* Scheme */
    start = ptr;

    if (*ptr == '/') {
        goto path;
    } else if (!(*ptr >= 'a' && *ptr <= 'z')
            && !(*ptr >= 'A' && *ptr <= 'Z')
            && *ptr != '%') {
        http_set_error("invalid first character \\%hhu in scheme",
                       (unsigned char)*ptr);
        return -1;
    }

    for (;;) {
        if (*ptr == ':' || *ptr == '\0') {
            toklen = (size_t)(ptr - start);
            uri->scheme = http_uri_decode_component(start, toklen);
            if (!uri->scheme)
                return -1;

            break;
        } else if (!http_uri_is_scheme_char((unsigned char)*ptr)
                && *ptr != '%') {
            http_set_error("invalid character \\%hhu in scheme",
                           (unsigned char)*ptr);
            return -1;
        }

        ptr++;
    }

    /* Skip '://' */
    if (ptr[0] != ':' || ptr[1] != '/' || ptr[2] != '/') {
        http_set_error("invalid characters after scheme");
        return -1;
    }

    ptr += 3;

    /* User (optional) */
    start = ptr;
    colon = NULL;
    while (*ptr != '\0') {
        if (*ptr == ':') {
            colon = ptr;
        } else if (*ptr == '@') {
            if (colon) {
                toklen = (size_t)(colon - start);
            } else {
                toklen = (size_t)(ptr - start);
            }

            uri->user = http_uri_decode_component(start, toklen);
            if (!uri->user)
                return -1;

            if (colon)
                ptr = colon;
            break;
        } else if (*ptr == '/') {
            /* End of authority, no user found */
            break;
        }

        ptr++;
    }

    if (!uri->user) {
        /* Since we did not find a username, we backtrack to read the host. */
        ptr = start;
    }

    /* Password (optional) */
    if (uri->user && *ptr == ':') {
        start = ptr;

        for (;;) {
            if (*ptr == '@' || *ptr == '\0') {
                toklen = (size_t)(ptr - start - 1);
                if (toklen == 0) {
                    http_set_error("empty password");
                    return -1;
                }

                uri->password = http_uri_decode_component(start + 1, toklen);
                if (!uri->password)
                    return -1;

                break;
            } else if (*ptr == '/') {
                /* End of authority, no password found */
                break;
            }

            ptr++;
        }

        if (!uri->password) {
            http_set_error("empty password");
            return -1;
        }
    }

    if (uri->user) {
        /* Skip '@' */
        ptr++;
    }

    /* Host */
    start = ptr;
    if (*start >= '0' && *start <= '9') {
        /* IPv4 address */
        for (;;) {
            if (*ptr == '/' || *ptr == ':' || *ptr == '\0') {
                toklen = (size_t)(ptr - start);
                if (toklen == 0) {
                    http_set_error("empty host");
                    return -1;
                }

                uri->host = http_uri_decode_component(start, toklen);
                if (!uri->host)
                    return -1;

                break;
            } else if (!http_uri_is_ipv4_addr_char((unsigned char)*ptr)) {
                http_set_error("invalid character \\%hhu in ipv4 address",
                               (unsigned char)*ptr);
                return -1;
            }

            ptr++;
        }
    } else if (*start == '[') {
        ptr++; /* '[' */
        start = ptr;

        /* IPv6 address */
        for (;;) {
            if (*ptr == ']') {
                toklen = (size_t)(ptr - start);
                if (toklen == 0) {
                    http_set_error("empty host");
                    return -1;
                }

                uri->host = http_uri_decode_component(start, toklen);
                if (!uri->host)
                    return -1;

                ptr++; /* ']' */

                break;
            } else if (*ptr == '\0') {
                http_set_error("truncated ipv6 address");
                return -1;
            } else if (!http_uri_is_ipv6_addr_char((unsigned char)*ptr)) {
                http_set_error("invalid character \\%hhu in ipv6 address",
                               (unsigned char)*ptr);
                return -1;
            }

            ptr++;
        }
    } else {
        /* Hostname */
        for (;;) {
            if (*ptr == '/' || *ptr == ':' || *ptr == '#' || *ptr == '\0') {
                toklen = (size_t)(ptr - start);
                if (toklen == 0) {
                    http_set_error("empty host");
                    return -1;
                }

                uri->host = http_uri_decode_component(start, toklen);
                if (!uri->host)
                    return -1;

                break;
            }

            ptr++;
        }
    }

    /* Port (optional) */
    if (*ptr == ':') {
        ptr++;

        start = ptr;

        for (;;) {
            if (*ptr == '/' || *ptr == '#' || *ptr == '\0') {
                toklen = (size_t)(ptr - start);
                if (toklen == 0) {
                    http_set_error("empty port");
                    return -1;
                }

                uri->port = http_uri_decode_component(start, toklen);
                if (!uri->port)
                    return -1;

                break;
            } else if (!http_uri_is_port_char((unsigned char)*ptr)) {
                http_set_error("invalid character \\%hhu in port",
                               (unsigned char)*ptr);
                return -1;
            }

            ptr++;
        }
    }

    /* Path (optional, default '/') */
path:
    if (*ptr == '/') {
        start = ptr;

        for (;;) {
            if (*ptr == '?' || *ptr == '#' || *ptr == '\0') {
                toklen = (size_t)(ptr - start);
                uri->path = http_uri_decode_component(start, toklen);
                if (!uri->path)
                    return -1;

                break;
            }

            ptr++;
        }
    } else {
        uri->path = http_strdup("/");
    }

    /* Query (optional) */
    if (*ptr == '?') {
        char *query;

        ptr++;

        start = ptr;

        while (*ptr != '#' && *ptr != '\0')
            ptr++;

        toklen = (size_t)(ptr - start);
        query = http_strndup(start, toklen);

        if (http_query_parameters_parse(query,
                                        &uri->query_parameters,
                                        &uri->nb_query_parameters) == -1) {
            http_free(query);
            return -1;
        }

        http_free(query);
    }

    /* Fragment (optional) */
    if (*ptr == '#') {
        ptr++;

        start = ptr;

        while (*ptr != '\0')
            ptr++;

        toklen = (size_t)(ptr - start);
        uri->fragment = http_uri_decode_component(start, toklen);
        if (!uri->fragment)
            return -1;
    }

    if (http_uri_finalize(uri) == -1)
        return -1;

    return 1;
}
示例#29
0
文件: network.c 项目: C3MA/fc_mplayer
int
http_send_request( URL_t *url, int64_t pos ) {
	HTTP_header_t *http_hdr;
	URL_t *server_url;
	char str[256];
	int fd = -1;
	int ret;
	int proxy = 0;		// Boolean

	http_hdr = http_new_header();

	if( !strcasecmp(url->protocol, "http_proxy") ) {
		proxy = 1;
		server_url = url_new( (url->file)+1 );
		if (!server_url) {
			mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
			goto err_out;
		}
		http_set_uri( http_hdr, server_url->noauth_url );
	} else {
		server_url = url;
		http_set_uri( http_hdr, server_url->file );
	}
	if (server_url->port && server_url->port != 80)
	    snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
	else
	    snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
	http_set_field( http_hdr, str);
	if (network_useragent)
	    snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
	else
	    snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
        http_set_field(http_hdr, str);

	if (network_referrer) {
	    char *referrer = NULL;
	    size_t len = strlen(network_referrer) + 10;

	    // Check len to ensure we don't do something really bad in case of an overflow
	    if (len > 10)
		referrer = malloc(len);

	    if (referrer == NULL) {
		mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MemAllocFailed);
	    } else {
		snprintf(referrer, len, "Referer: %s", network_referrer);
		http_set_field(http_hdr, referrer);
		free(referrer);
	    }
	}

	if( strcasecmp(url->protocol, "noicyx") )
	    http_set_field(http_hdr, "Icy-MetaData: 1");

	if(pos>0) {
	// Extend http_send_request with possibility to do partial content retrieval
	    snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
	    http_set_field(http_hdr, str);
	}

	if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );

	if (network_http_header_fields) {
		int i=0;
		while (network_http_header_fields[i])
			http_set_field(http_hdr, network_http_header_fields[i++]);
	}

	http_set_field( http_hdr, "Connection: close");
	if (proxy)
		http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
	http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
	if( http_build_request( http_hdr )==NULL ) {
		goto err_out;
	}

	if( proxy ) {
		if( url->port==0 ) url->port = 8080;			// Default port for the proxy server
		fd = connect2Server( url->hostname, url->port,1 );
		url_free( server_url );
		server_url = NULL;
	} else {
		if( server_url->port==0 ) server_url->port = 80;	// Default port for the web server
		fd = connect2Server( server_url->hostname, server_url->port,1 );
	}
	if( fd<0 ) {
		goto err_out;
	}
	mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );

	ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
	if( ret!=(int)http_hdr->buffer_size ) {
		mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
		goto err_out;
	}

	http_free( http_hdr );

	return fd;
err_out:
	if (fd > 0) closesocket(fd);
	http_free(http_hdr);
	if (proxy && server_url)
		url_free(server_url);
	return -1;
}
示例#30
0
int  http_seek(stream_t *stream, off_t pos) 
{

	HTTP_header_t * http_hdr = NULL;
	int fd = -1;

	if (stream == NULL)
	{
		OS_PRINTF("[%s][ERROR] stream == NULL!!!!\n",__func__);
		return 0;
	}
	
	//This if removed for network play issue fix, Gavin 2013-09-09
	//if (stream->fd > 0) closesocket(stream->fd); // need to reconnect to seek in http-stream
	//yliu add :for  fd = 0
#ifdef __LINUX__
	if (stream->fd > 0) 
	{
		closesocket(stream->fd); // need to reconnect to seek in http-stream
	}
#else
	if (stream->fd >= 0) 
	{
		closesocket(stream->fd);
		stream->fd = -1;
	}
#endif
      
	 if(is_file_seq_exit())
	 {
       
	     return -1;
	 }

	fd = http_send_request(stream->streaming_ctrl->url, pos);

	if (fd < 0) 
	{
		OS_PRINTF("[%s][ERROR] fail to send request  fd[%d]!!!!!!!!\n",__func__,fd);
		return 0;
	}

	http_hdr = http_read_response(fd);
       
	if (http_hdr == NULL) 
	{
	//yliu add:
	 closesocket(fd);
       fd = -1;
		OS_PRINTF("[%s][ERROR] fail to read response !!!!!!!!\n",__func__);
		return 0;
	}
      
      stream->streaming_ctrl->chunksize = http_hdr->chunksize;
	if (mp_msg_test(MSGT_NETWORK, MSGL_V))
	{
		http_debug_hdr(http_hdr);
	}

	switch (http_hdr->status_code) {
		case 200:
		case 206: // OK
			//mp_msg(MSGT_NETWORK, MSGL_V, "Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type"));
			//mp_msg(MSGT_NETWORK, MSGL_V, "Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length"));
			OS_PRINTF("[%s] Content-Type: [%s]\n",__func__,http_get_field(http_hdr, "Content-Type"));
			OS_PRINTF("[%s] Content-Length: [%s]\n",__func__,http_get_field(http_hdr, "Content-Length"));
                   //  yliu add :for reconnect
                   //if(pos == 0)
                    {
              	if(http_get_field(http_hdr, "Content-Length"))
			   stream->end_pos = atoll(http_get_field(http_hdr, "Content-Length"))+pos;
                   else
                      stream->end_pos = 0;
                    }
			OS_PRINTF("[%s] stream->end_pos: [%ld]\n",__func__,stream->end_pos);
			
			if (http_hdr->body_size > 0) {
				if (streaming_bufferize(stream->streaming_ctrl, http_hdr->body, http_hdr->body_size) < 0) {
					http_free(http_hdr);
                    closesocket(fd);
                    fd = -1;
					return -1;
				}
			}

			break;

			
		default:
			mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase);
			closesocket(fd);
			fd = -1;
	}
	//add macro for consistency in Linux Version, yliu 2013-09-10
#if  0
	//This if added for network play issue fix, Gavin 2013-09-09
	if(stream->fd >= 0)
	{
		closesocket(stream->fd); // need to reconnect to seek in http-stream
		stream->fd = -1;
	}
#endif

	stream->fd = fd;

	if (http_hdr) {
		
		http_free(http_hdr);
		stream->streaming_ctrl->data = NULL;
		
	}

	stream->pos = pos;
	return 1;
}