Exemple #1
0
inline void send_header(struct ir_remote *remote)
{
	if (has_header(remote)) {
		send_pulse(remote->phead);
		send_space(remote->shead);
	}
}
Exemple #2
0
 string RequestImpl::get_header( const string& name, const string& default_value ) const
 {
     string value = default_value;
     
     if ( has_header( name ) )
     {
         const auto iterator = Map::find_key_ignoring_case( name, m_headers );
         
         value = iterator->second;
     }
     
     return value;
 }
Exemple #3
0
void fprint_remote_head(FILE *f, struct ir_remote *rem)
{
	fprintf(f, "begin remote\n\n");
	if(!is_raw(rem)){
		fprintf(f, "  name  %s\n",rem->name);
		fprintf(f, "  bits        %5d\n",rem->bits);
		fprint_flags(f,rem->flags);
		fprintf(f, "  eps         %5d\n",rem->eps);
		fprintf(f, "  aeps        %5d\n\n",rem->aeps);
		if(has_header(rem))
		{
			fprintf(f, "  header      %5lu %5lu\n",
				(unsigned long) rem->phead,
				(unsigned long) rem->shead);
		}
		if(rem->pthree!=0 || rem->sthree!=0)
			fprintf(f, "  three       %5lu %5lu\n",
				(unsigned long) rem->pthree,
				(unsigned long) rem->sthree);
		if(rem->ptwo!=0 || rem->stwo!=0)
			fprintf(f, "  two         %5lu %5lu\n",
				(unsigned long) rem->ptwo,
				(unsigned long)  rem->stwo);
		fprintf(f, "  one         %5lu %5lu\n",
			(unsigned long) rem->pone,
			(unsigned long) rem->sone);
		fprintf(f, "  zero        %5lu %5lu\n",
			(unsigned long) rem->pzero,
			(unsigned long)  rem->szero);
		if(rem->ptrail!=0)
		{
			fprintf(f, "  ptrail      %5lu\n",
				(unsigned long) rem->ptrail);
		}
		if(rem->plead!=0)
		{
			fprintf(f, "  plead       %5lu\n",
				(unsigned long) rem->plead);
		}
		if(has_foot(rem))
		{
			fprintf(f, "  foot        %5lu %5lu\n",
				(unsigned long) rem->pfoot,
				(unsigned long) rem->sfoot);
		}
		if(has_repeat(rem))
		{
			fprintf(f, "  repeat      %5lu %5lu\n",
				(unsigned long) rem->prepeat,
				(unsigned long) rem->srepeat);
		}
		if(rem->pre_data_bits>0)
		{
			fprintf(f, "  pre_data_bits   %d\n",rem->pre_data_bits);
#                       ifdef LONG_IR_CODE
			fprintf(f, "  pre_data       0x%llX\n",rem->pre_data);
#                       else
			fprintf(f, "  pre_data       0x%lX\n",rem->pre_data);
#                       endif
		}
		if(rem->post_data_bits>0)
		{
			fprintf(f, "  post_data_bits  %d\n",rem->post_data_bits);
#                       ifdef LONG_IR_CODE
			fprintf(f, "  post_data      0x%llX\n",rem->post_data);
#                       else
			fprintf(f, "  post_data      0x%lX\n",rem->post_data);
#                       endif
		}
		if(rem->pre_p!=0 && rem->pre_s!=0)
		{
			fprintf(f, "  pre         %5lu %5lu\n",
				(unsigned long) rem->pre_p,
				(unsigned long) rem->pre_s);
		}
		if(rem->post_p!=0 && rem->post_s!=0)
		{
			fprintf(f, "  post        %5lu %5lu\n",
				(unsigned long) rem->post_p,
				(unsigned long) rem->post_s);
		}
		fprintf(f, "  gap          %lu\n",
			(unsigned long) rem->gap);
		if(has_repeat_gap(rem))
		{
			fprintf(f, "  repeat_gap   %lu\n",
				(unsigned long) rem->repeat_gap);
		}
		if(rem->min_repeat>0)
		{
			fprintf(f, "  min_repeat      %d\n",rem->min_repeat);
		}
		if(rem->min_code_repeat>0)
		{
			fprintf(f, "  min_code_repeat %d\n",
				rem->min_code_repeat);
		}
#               ifdef LONG_IR_CODE
		fprintf(f, "  toggle_bit_mask 0x%llX\n",
			rem->toggle_bit_mask);
#               else
		fprintf(f, "  toggle_bit_mask 0x%lX\n",
			rem->toggle_bit_mask);
#               endif
		if(has_toggle_mask(rem))
		{
#                       ifdef LONG_IR_CODE
			fprintf(f, "  toggle_mask    0x%llX\n",
				rem->toggle_mask);
#                       else
			fprintf(f, "  toggle_mask    0x%lX\n",
				rem->toggle_mask);
#                       endif
		}
		if(rem->rc6_mask!=0)
		{
#                       ifdef LONG_IR_CODE
			fprintf(f, "  rc6_mask    0x%llX\n",
				rem->rc6_mask);
#                       else
			fprintf(f, "  rc6_mask    0x%lX\n",
				rem->rc6_mask);
#                       endif
		}
		if(is_serial(rem))
		{
			fprintf(f, "  baud            %d\n",rem->baud);
			fprintf(f, "  serial_mode     %dN%d%s\n",
				rem->bits_in_byte,
				rem->stop_bits/2,
				rem->stop_bits%2 ? ".5":"");
		}
	}
	else
	{
		fprintf(f, "  name   %s\n",rem->name);
		fprint_flags(f,rem->flags);
		fprintf(f, "  eps         %5d\n",rem->eps);
		fprintf(f, "  aeps        %5d\n\n",rem->aeps);
		fprintf(f, "  ptrail      %5lu\n",(unsigned long) rem->ptrail);
		fprintf(f, "  repeat %5lu %5lu\n",
			(unsigned long) rem->prepeat,
			(unsigned long) rem->srepeat);
		fprintf(f, "  gap    %lu\n",(unsigned long) rem->gap);
	}
	if(rem->freq!=0)
	{
		fprintf(f, "  frequency    %u\n",rem->freq);
	}
	if(rem->duty_cycle!=0)
	{
		fprintf(f, "  duty_cycle   %u\n",rem->duty_cycle);
	}
	fprintf(f,"\n");
}
bool HttpResponse::has_range() const
{
    return has_header("Content-Range");
}
Exemple #5
0
int receive_decode(struct ir_remote *remote,
		   ir_code *prep,ir_code *codep,ir_code *postp,
		   int *repeat_flagp,lirc_t *remaining_gapp)
{
	ir_code pre,code,post,code_mask=0,post_mask=0;
	lirc_t sync;
	int header;
	struct timeval current;

	sync=0; /* make compiler happy */
	code=pre=post=0;
	header=0;

	if(hw.rec_mode==LIRC_MODE_MODE2 ||
	   hw.rec_mode==LIRC_MODE_PULSE ||
	   hw.rec_mode==LIRC_MODE_RAW)
	{
		rewind_rec_buffer();
		rec_buffer.is_biphase=is_biphase(remote) ? 1:0;
		
		/* we should get a long space first */
		if(!(sync=sync_rec_buffer(remote)))
		{
			LOGPRINTF(1,"failed on sync");
			return(0);
		}

		LOGPRINTF(1,"sync");

		if(has_repeat(remote) && last_remote==remote)
		{
			if(remote->flags&REPEAT_HEADER && has_header(remote))
			{
				if(!get_header(remote))
				{
					LOGPRINTF(1,"failed on repeat "
						  "header");
					return(0);
				}
				LOGPRINTF(1,"repeat header");
			}
			if(get_repeat(remote))
			{
				if(remote->last_code==NULL)
				{
					logprintf(LOG_NOTICE,"repeat code "
						  "without last_code "
						  "received");
					return(0);
				}

				*prep=remote->pre_data;
				*codep=remote->last_code->code;
				*postp=remote->post_data;
				*repeat_flagp=1;

				*remaining_gapp=
				is_const(remote) ? 
				(remote->gap>rec_buffer.sum ?
				 remote->gap-rec_buffer.sum:0):
				(has_repeat_gap(remote) ?
				 remote->repeat_gap:remote->gap);
				return(1);
			}
			else
			{
				LOGPRINTF(1,"no repeat");
				rewind_rec_buffer();
				sync_rec_buffer(remote);
			}

		}

		if(has_header(remote))
		{
			header=1;
			if(!get_header(remote))
			{
				header=0;
				if(!(remote->flags&NO_HEAD_REP && 
				     (sync<=remote->gap+remote->gap*remote->eps/100
				      || sync<=remote->gap+remote->aeps)))
				{
					LOGPRINTF(1,"failed on header");
					return(0);
				}
			}
			LOGPRINTF(1,"header");
		}
	}

	if(is_raw(remote))
	{
		struct ir_ncode *codes,*found;
		int i;

		if(hw.rec_mode==LIRC_MODE_CODE ||
		   hw.rec_mode==LIRC_MODE_LIRCCODE)
			return(0);

		codes=remote->codes;
		found=NULL;
		while(codes->name!=NULL && found==NULL)
		{
			found=codes;
			for(i=0;i<codes->length;)
			{
				if(!expectpulse(remote,codes->signals[i++]))
				{
					found=NULL;
					rewind_rec_buffer();
					sync_rec_buffer(remote);
					break;
				}
				if(i<codes->length &&
				   !expectspace(remote,codes->signals[i++]))
				{
					found=NULL;
					rewind_rec_buffer();
					sync_rec_buffer(remote);
					break;
				}
			}
			codes++;
		}
		if(found!=NULL)
		{
			if(!get_gap(remote,
				    is_const(remote) ? 
				    remote->gap-rec_buffer.sum:
				    remote->gap)) 
				found=NULL;
		}
		if(found==NULL) return(0);
		code=found->code;
	}
	else
	{
		if(hw.rec_mode==LIRC_MODE_CODE ||
		   hw.rec_mode==LIRC_MODE_LIRCCODE)
		{
			int i;
 			lirc_t sum;

#                       ifdef LONG_IR_CODE
			LOGPRINTF(1,"decoded: %llx",rec_buffer.decoded);
#                       else
			LOGPRINTF(1,"decoded: %lx",rec_buffer.decoded);
#                       endif
			if((hw.rec_mode==LIRC_MODE_CODE &&
			    hw.code_length<remote->pre_data_bits
			    +remote->bits+remote->post_data_bits)
			   ||
			   (hw.rec_mode==LIRC_MODE_LIRCCODE && 
			    hw.code_length!=remote->pre_data_bits
			    +remote->bits+remote->post_data_bits))
			{
				return(0);
			}
			
			for(i=0;i<remote->post_data_bits;i++)
			{
				post_mask=(post_mask<<1)+1;
			}
			post=rec_buffer.decoded&post_mask;
			post_mask=0;
			rec_buffer.decoded=
			rec_buffer.decoded>>remote->post_data_bits;
			for(i=0;i<remote->bits;i++)
			{
				code_mask=(code_mask<<1)+1;
			}
			code=rec_buffer.decoded&code_mask;
			code_mask=0;
			pre=rec_buffer.decoded>>remote->bits;
			gettimeofday(&current,NULL);
			sum=remote->phead+remote->shead+
				lirc_t_max(remote->pone+remote->sone,
					   remote->pzero+remote->szero)*
				(remote->bits+
				 remote->pre_data_bits+
				 remote->post_data_bits)+
				remote->plead+
				remote->ptrail+
				remote->pfoot+remote->sfoot+
				remote->pre_p+remote->pre_s+
				remote->post_p+remote->post_s;
			
			rec_buffer.sum=sum>=remote->gap ? remote->gap-1:sum;
			sync=time_elapsed(&remote->last_send,&current)-
 				rec_buffer.sum;
		}
		else
		{
			if(!get_lead(remote))
static void rm_task(const char* src,const char* dst)
{
	FILE* f;
	FILE* out;
	uint8_t* block;
	uint32_t fsize;

	printf("Processing %s -> %s\n",src,dst);

	f = fopen(src,"rb");

	if(!f)
	{
		printf("%s not found\n",src);
		return;
	}

	fsize = file_size(f);

	if(!fsize)
	{
		printf("%s is empty\n",src);
		fclose(f);
		return;
	}
	
	if(!has_header(fsize))
	{
		printf("%s does not contain header.Aborting\n",src);
		fclose(f);
		return;
	}
	
	printf("%s contains header!\n",src);

	out = fopen(dst,"wb");

	if(!out)
	{
		printf("Can't open %s\n",dst);
		fclose(f);
		return;
	}

	printf("Allocating block..\n");
	block = file_to_mem(f,0x200,fsize - 0x200); /*lazy :D*/

	if(!block)
	{
		printf("Out of memory\n");
		fclose(f);
		fclose(out);
		return;
	}
	
	printf("Writing %s..\n",dst);	
	fwrite(block,1,fsize - 0x200,out);

	printf("Job done!\n");
	free(block);
	fclose(f);
	fclose(out);
}
Exemple #7
0
void fprint_remote_head(FILE* f, const struct ir_remote* rem)
{
	fprintf(f, "begin remote\n\n");
	fprintf(f, "  name  %s\n", rem->name);
	if (rem->manual_sort)
		fprintf(f, "  manual_sort  %d\n", rem->manual_sort);
	if (rem->driver)
		fprintf(f, "  driver %s\n", rem->driver);
	if (!is_raw(rem))
		fprintf(f, "  bits        %5d\n", rem->bits);
	fprint_flags(f, rem->flags);
	fprintf(f, "  eps         %5d\n", rem->eps);
	fprintf(f, "  aeps        %5d\n\n", rem->aeps);
	if (!is_raw(rem)) {
		if (has_header(rem))
			fprintf(f, "  header      %5u %5u\n", (__u32)rem->phead, (__u32)rem->shead);
		if (rem->pthree != 0 || rem->sthree != 0)
			fprintf(f, "  three       %5u %5u\n", (__u32)rem->pthree, (__u32)rem->sthree);
		if (rem->ptwo != 0 || rem->stwo != 0)
			fprintf(f, "  two         %5u %5u\n", (__u32)rem->ptwo, (__u32)rem->stwo);
		fprintf(f, "  one         %5u %5u\n", (__u32)rem->pone, (__u32)rem->sone);
		fprintf(f, "  zero        %5u %5u\n", (__u32)rem->pzero, (__u32)rem->szero);
	}
	if (rem->ptrail != 0)
		fprintf(f, "  ptrail      %5u\n", (__u32)rem->ptrail);
	if (!is_raw(rem)) {
		if (rem->plead != 0)
			fprintf(f, "  plead       %5u\n", (__u32)rem->plead);
		if (has_foot(rem))
			fprintf(f, "  foot        %5u %5u\n", (__u32)rem->pfoot, (__u32)rem->sfoot);
	}
	if (has_repeat(rem))
		fprintf(f, "  repeat      %5u %5u\n", (__u32)rem->prepeat, (__u32)rem->srepeat);
	if (!is_raw(rem)) {
		if (rem->pre_data_bits > 0) {
			fprintf(f, "  pre_data_bits   %d\n", rem->pre_data_bits);
			fprintf(f, "  pre_data       0x%llX\n", (unsigned long long)rem->pre_data);
		}
		if (rem->post_data_bits > 0) {
			fprintf(f, "  post_data_bits  %d\n", rem->post_data_bits);
			fprintf(f, "  post_data      0x%llX\n", (unsigned long long)rem->post_data);
		}
		if (rem->pre_p != 0 && rem->pre_s != 0)
			fprintf(f, "  pre         %5u %5u\n", (__u32)rem->pre_p, (__u32)rem->pre_s);
		if (rem->post_p != 0 && rem->post_s != 0)
			fprintf(f, "  post        %5u %5u\n", (__u32)rem->post_p, (__u32)rem->post_s);
	}
	fprint_remote_gap(f, rem);
	if (has_repeat_gap(rem))
		fprintf(f, "  repeat_gap   %u\n", (__u32)rem->repeat_gap);
	if (rem->suppress_repeat > 0)
		fprintf(f, "  suppress_repeat %d\n", rem->suppress_repeat);
	if (rem->min_repeat > 0) {
		fprintf(f, "  min_repeat      %d\n", rem->min_repeat);
		if (rem->suppress_repeat == 0) {
			fprintf(f, "#  suppress_repeat %d\n", rem->min_repeat);
			fprintf(f, "#  uncomment to suppress unwanted repeats\n");
		}
	}
	if (!is_raw(rem)) {
		if (rem->min_code_repeat > 0)
			fprintf(f, "  min_code_repeat %d\n", rem->min_code_repeat);
		fprintf(f, "  toggle_bit_mask 0x%llX\n", (unsigned long long)rem->toggle_bit_mask);
		if (has_toggle_mask(rem))
			fprintf(f, "  toggle_mask    0x%llX\n", (unsigned long long)rem->toggle_mask);
		if (rem->repeat_mask != 0)
			fprintf(f, "  repeat_mask    0x%llX\n", (unsigned long long)rem->repeat_mask);
		if (rem->rc6_mask != 0)
			fprintf(f, "  rc6_mask    0x%llX\n", (unsigned long long)rem->rc6_mask);
		if (has_ignore_mask(rem))
			fprintf(f, "  ignore_mask 0x%llX\n", (unsigned long long)rem->ignore_mask);
		if (is_serial(rem)) {
			fprintf(f, "  baud            %d\n", rem->baud);
			fprintf(f, "  serial_mode     %dN%d%s\n", rem->bits_in_byte, rem->stop_bits / 2,
				rem->stop_bits % 2 ? ".5" : "");
		}
	}
	if (rem->freq != 0)
		fprintf(f, "  frequency    %u\n", rem->freq);
	if (rem->duty_cycle != 0)
		fprintf(f, "  duty_cycle   %u\n", rem->duty_cycle);
	fprintf(f, "\n");
}
Exemple #8
0
int init_send(struct ir_remote *remote,struct ir_ncode *code)
{
	int i, repeat=0;
	
	if(is_grundig(remote) || 
	   is_goldstar(remote) || is_serial(remote) || is_bo(remote))
	{
		logprintf(LOG_ERR,"sorry, can't send this protocol yet");
		return(0);
	}
	clear_send_buffer();
	if(is_biphase(remote))
	{
		send_buffer.is_biphase=1;
	}
	if(repeat_remote==NULL)
	{
		remote->repeat_countdown=remote->min_repeat;
	}
	else
	{
		repeat = 1;
	}
	
 init_send_loop:
	if(repeat && has_repeat(remote))
	{
		if(remote->flags&REPEAT_HEADER && has_header(remote))
		{
			send_header(remote);
		}
		send_repeat(remote);
	}
	else
	{
		if(!is_raw(remote))
		{
			ir_code next_code;
			
			if(code->transmit_state == NULL)
			{
				next_code = code->code;
			}
			else
			{
				next_code = code->transmit_state->code;
			}
			send_code(remote, next_code, repeat);
			if(has_toggle_mask(remote))
			{
				remote->toggle_mask_state++;
				if(remote->toggle_mask_state==4)
				{
					remote->toggle_mask_state=2;
				}
			}
			send_buffer.data=send_buffer._data;
		}
		else
		{
			if(code->signals==NULL)
			{
				logprintf(LOG_ERR, "no signals for raw send");
				return 0;
			}
			if(send_buffer.wptr>0)
			{
				send_signals(code->signals, code->length);
			}
			else
			{
				send_buffer.data=code->signals;
				send_buffer.wptr=code->length;
				for(i=0; i<code->length; i++)
				{
					send_buffer.sum+=code->signals[i];
				}
			}
		}
	}
	sync_send_buffer();
	if(bad_send_buffer())
	{
		logprintf(LOG_ERR,"buffer too small");
		return(0);
	}
	if(has_repeat_gap(remote) && repeat && has_repeat(remote))
	{
		remote->min_remaining_gap=remote->repeat_gap;
		remote->max_remaining_gap=remote->repeat_gap;
	}
	else if(is_const(remote))
	{
		if(min_gap(remote)>send_buffer.sum)
		{
			remote->min_remaining_gap=min_gap(remote)-send_buffer.sum;
			remote->max_remaining_gap=max_gap(remote)-send_buffer.sum;
		}
		else
		{
			logprintf(LOG_ERR,"too short gap: %u",remote->gap);
			remote->min_remaining_gap=min_gap(remote);
			remote->max_remaining_gap=max_gap(remote);
			return(0);
		}
	}
	else
	{
		remote->min_remaining_gap=min_gap(remote);
		remote->max_remaining_gap=max_gap(remote);
	}
	/* update transmit state */
	if(code->next != NULL)
	{
		if(code->transmit_state == NULL)
		{
			code->transmit_state = code->next;
		}
		else
		{
			code->transmit_state = code->transmit_state->next;
		}
	}
	if((remote->repeat_countdown>0 || code->transmit_state != NULL) &&
	   remote->min_remaining_gap<LIRCD_EXACT_GAP_THRESHOLD)
	{
		if(send_buffer.data!=send_buffer._data)
		{
			lirc_t *signals;
			int n;
			
			LOGPRINTF(1, "unrolling raw signal optimisation");
			signals=send_buffer.data;
			n=send_buffer.wptr;
			send_buffer.data=send_buffer._data;
			send_buffer.wptr=0;
			
			send_signals(signals, n);
		}
		LOGPRINTF(1, "concatenating low gap signals");
		if(code->next == NULL || code->transmit_state == NULL)
		{
			remote->repeat_countdown--;
		}
		send_space(remote->min_remaining_gap);
		flush_send_buffer();
		send_buffer.sum=0;
		
		repeat = 1;
		goto init_send_loop;
	}
	LOGPRINTF(3, "transmit buffer ready");
	return(1);
}
Exemple #9
0
static int http_connect(URLContext *h, const char *path, const char *local_path,
                        const char *hoststr, const char *auth,
                        const char *proxyauth, int *new_location)
{
    HTTPContext *s = h->priv_data;
    int post, err;
    char headers[HTTP_HEADERS_SIZE] = "";
    char *authstr = NULL, *proxyauthstr = NULL;
    int64_t off = s->off;
    int len = 0;
    const char *method;
    int send_expect_100 = 0;

    /* send http header */
    post = h->flags & AVIO_FLAG_WRITE;

    if (s->post_data) {
        /* force POST method and disable chunked encoding when
         * custom HTTP post data is set */
        post            = 1;
        s->chunked_post = 0;
    }

    if (s->method)
        method = s->method;
    else
        method = post ? "POST" : "GET";

    authstr      = ff_http_auth_create_response(&s->auth_state, auth,
                                                local_path, method);
    proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
                                                local_path, method);
    if (post && !s->post_data) {
        send_expect_100 = s->send_expect_100;
        /* The user has supplied authentication but we don't know the auth type,
         * send Expect: 100-continue to get the 401 response including the
         * WWW-Authenticate header, or an 100 continue if no auth actually
         * is needed. */
        if (auth && *auth &&
            s->auth_state.auth_type == HTTP_AUTH_NONE &&
            s->http_code != 401)
            send_expect_100 = 1;
    }

    /* set default headers if needed */
    if (!has_header(s->headers, "\r\nUser-Agent: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "User-Agent: %s\r\n", s->user_agent);
    if (!has_header(s->headers, "\r\nAccept: "))
        len += av_strlcpy(headers + len, "Accept: */*\r\n",
                          sizeof(headers) - len);
    // Note: we send this on purpose even when s->off is 0 when we're probing,
    // since it allows us to detect more reliably if a (non-conforming)
    // server supports seeking by analysing the reply headers.
    if (!has_header(s->headers, "\r\nRange: ") && !post && (s->off > 0 || s->end_off || s->seekable == -1)) {
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Range: bytes=%"PRId64"-", s->off);
        if (s->end_off)
            len += av_strlcatf(headers + len, sizeof(headers) - len,
                               "%"PRId64, s->end_off - 1);
        len += av_strlcpy(headers + len, "\r\n",
                          sizeof(headers) - len);
    }
    if (send_expect_100 && !has_header(s->headers, "\r\nExpect: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Expect: 100-continue\r\n");

    if (!has_header(s->headers, "\r\nConnection: ")) {
        if (s->multiple_requests)
            len += av_strlcpy(headers + len, "Connection: keep-alive\r\n",
                              sizeof(headers) - len);
        else
            len += av_strlcpy(headers + len, "Connection: close\r\n",
                              sizeof(headers) - len);
    }

    if (!has_header(s->headers, "\r\nHost: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Host: %s\r\n", hoststr);
    if (!has_header(s->headers, "\r\nContent-Length: ") && s->post_data)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Content-Length: %d\r\n", s->post_datalen);

    if (!has_header(s->headers, "\r\nContent-Type: ") && s->content_type)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Content-Type: %s\r\n", s->content_type);
    if (!has_header(s->headers, "\r\nCookie: ") && s->cookies) {
        char *cookies = NULL;
        if (!get_cookies(s, &cookies, path, hoststr) && cookies) {
            len += av_strlcatf(headers + len, sizeof(headers) - len,
                               "Cookie: %s\r\n", cookies);
            av_free(cookies);
        }
    }
    if (!has_header(s->headers, "\r\nIcy-MetaData: ") && s->icy)
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Icy-MetaData: %d\r\n", 1);

    /* now add in custom headers */
    if (s->headers)
        av_strlcpy(headers + len, s->headers, sizeof(headers) - len);

    snprintf(s->buffer, sizeof(s->buffer),
             "%s %s HTTP/1.1\r\n"
             "%s"
             "%s"
             "%s"
             "%s%s"
             "\r\n",
             method,
             path,
             post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
             headers,
             authstr ? authstr : "",
             proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");

    av_log(h, AV_LOG_DEBUG, "request: %s\n", s->buffer);

    if ((err = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
        goto done;

    if (s->post_data)
        if ((err = ffurl_write(s->hd, s->post_data, s->post_datalen)) < 0)
            goto done;

    /* init input buffer */
    s->buf_ptr          = s->buffer;
    s->buf_end          = s->buffer;
    s->line_count       = 0;
    s->off              = 0;
    s->icy_data_read    = 0;
    s->filesize         = -1;
    s->willclose        = 0;
    s->end_chunked_post = 0;
    s->end_header       = 0;
    if (post && !s->post_data && !send_expect_100) {
        /* Pretend that it did work. We didn't read any header yet, since
         * we've still to send the POST data, but the code calling this
         * function will check http_code after we return. */
        s->http_code = 200;
        err = 0;
        goto done;
    }

    /* wait for header */
    err = http_read_header(h, new_location);
    if (err < 0)
        goto done;

    if (*new_location)
        s->off = off;

    err = (off == s->off) ? 0 : -1;
done:
    av_freep(&authstr);
    av_freep(&proxyauthstr);
    return err;
}
Exemple #10
0
static int http_connect(URLContext *h, const char *path, const char *hoststr,
                        const char *auth, int *new_location)
{
    HTTPContext *s = h->priv_data;
    int post, err;
    char line[1024];
    char headers[1024] = "";
    char *authstr = NULL;
    int64_t off = s->off;
    int len = 0;


    /* send http header */
    post = h->flags & URL_WRONLY;
    authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
                                        post ? "POST" : "GET");

    /* set default headers if needed */
    if (!has_header(s->headers, "\r\nUser-Agent: "))
       len += av_strlcatf(headers + len, sizeof(headers) - len,
                          "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
    if (!has_header(s->headers, "\r\nAccept: "))
        len += av_strlcpy(headers + len, "Accept: */*\r\n",
                          sizeof(headers) - len);
    if (!has_header(s->headers, "\r\nRange: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Range: bytes=%"PRId64"-\r\n", s->off);
    if (!has_header(s->headers, "\r\nConnection: "))
        len += av_strlcpy(headers + len, "Connection: close\r\n",
                          sizeof(headers)-len);
    if (!has_header(s->headers, "\r\nHost: "))
        len += av_strlcatf(headers + len, sizeof(headers) - len,
                           "Host: %s\r\n", hoststr);

    /* now add in custom headers */
    av_strlcpy(headers+len, s->headers, sizeof(headers)-len);

    snprintf(s->buffer, sizeof(s->buffer),
             "%s %s HTTP/1.1\r\n"
             "%s"
             "%s"
             "%s"
             "\r\n",
             post ? "POST" : "GET",
             path,
             post && s->is_chunked ? "Transfer-Encoding: chunked\r\n" : "",
             headers,
             authstr ? authstr : "");

    av_freep(&authstr);
    if (url_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
        return AVERROR(EIO);

    /* init input buffer */
    s->buf_ptr = s->buffer;
    s->buf_end = s->buffer;
    s->line_count = 0;
    s->off = 0;
    s->filesize = -1;
    s->chunksize = -1;
    if (post) {
        /* always use chunked encoding for upload data */
        s->chunksize = 0;
        /* Pretend that it did work. We didn't read any header yet, since
         * we've still to send the POST data, but the code calling this
         * function will check http_code after we return. */
        s->http_code = 200;
        return 0;
    }

    /* wait for header */
    for(;;) {
        if (http_get_line(s, line, sizeof(line)) < 0)
            return AVERROR(EIO);

        dprintf(NULL, "header='%s'\n", line);

        err = process_line(h, line, s->line_count, new_location);
        if (err < 0)
            return err;
        if (err == 0)
            break;
        s->line_count++;
    }

    return (off == s->off) ? 0 : -1;
}