annotate libmpdemux/http.c @ 15265:92772475ed8d

toolame now works in vbr mode, too
author nicodvb
date Mon, 25 Apr 2005 10:37:55 +0000
parents 475c551d9890
children 281d155fb37f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
1 /*
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
2 * HTTP Helper
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
4 * (C) 2001, MPlayer team.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
5 */
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
6
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
7 #include <stdio.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
8 #include <stdlib.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
9 #include <string.h>
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
10
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
11 #include "http.h"
4816
f1dea39a50bb Fixed the http response parser when the http header only has the HTTP
bertrand
parents: 4311
diff changeset
12 #include "url.h"
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
13 #include "mp_msg.h"
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
14
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
15 HTTP_header_t *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
16 http_new_header() {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
17 HTTP_header_t *http_hdr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
18
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
19 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t));
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
20 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
21 memset( http_hdr, 0, sizeof(HTTP_header_t) );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
22
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
23 return http_hdr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
24 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
25
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
26 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
27 http_free( HTTP_header_t *http_hdr ) {
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
28 HTTP_field_t *field, *field2free;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
29 if( http_hdr==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
30 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
31 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
32 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
33 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
34 if( http_hdr->method!=NULL ) free( http_hdr->method );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
35 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
36 field = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
37 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
38 field2free = field;
14460
475c551d9890 free http field_name to fix memleak
reimar
parents: 12391
diff changeset
39 if (field->field_name)
475c551d9890 free http field_name to fix memleak
reimar
parents: 12391
diff changeset
40 free(field->field_name);
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
41 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
42 free( field2free );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
43 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
44 free( http_hdr );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
45 http_hdr = NULL;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
46 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
47
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
48 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
49 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
1027
2803b7076c83 Checked the length arg when appending data.
bertrand
parents: 902
diff changeset
50 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
7304
7da2c2a68547 Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents: 7293
diff changeset
51
7293
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
52 http_hdr->buffer = (char*)realloc( http_hdr->buffer, http_hdr->buffer_size+length+1 );
7304
7da2c2a68547 Check if realloc failed on http_hdr->buffer instead of ptr in http_response_append,
bertrand
parents: 7293
diff changeset
53 if( http_hdr->buffer==NULL ) {
7293
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
54 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory (re)allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
55 return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
56 }
7293
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
57 memcpy( http_hdr->buffer+http_hdr->buffer_size, response, length );
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
58 http_hdr->buffer_size += length;
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
59 http_hdr->buffer[http_hdr->buffer_size]=0; // close the string!
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
60 return http_hdr->buffer_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
61 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
62
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
63 int
2489
0ecc1b4f7cf8 Added ASF http server streaming (Not mms streaming).
bertrand
parents: 2310
diff changeset
64 http_is_header_entire( HTTP_header_t *http_hdr ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
65 if( http_hdr==NULL ) return -1;
7293
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
66 if( http_hdr->buffer==NULL ) return 0; // empty
0d7942100437 - simpler http_response_append (uses realloc())
arpi
parents: 6514
diff changeset
67
3784
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
68 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL &&
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
69 strstr(http_hdr->buffer, "\n\n")==NULL ) return 0;
8b7722329a27 warning fix == cleanup
arpi
parents: 3514
diff changeset
70 return 1;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
71 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
72
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
73 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
74 http_response_parse( HTTP_header_t *http_hdr ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
75 char *hdr_ptr, *ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
76 char *field=NULL;
8179
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
77 int pos_hdr_sep, hdr_sep_len, len;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
78 if( http_hdr==NULL ) return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
79 if( http_hdr->is_parsed ) return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
80
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
81 // Get the protocol
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
82 hdr_ptr = strstr( http_hdr->buffer, " " );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
83 if( hdr_ptr==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
84 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. No space separator found.\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
85 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
86 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
87 len = hdr_ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
88 http_hdr->protocol = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
89 if( http_hdr->protocol==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
90 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
91 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
92 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
93 strncpy( http_hdr->protocol, http_hdr->buffer, len );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
94 http_hdr->protocol[len]='\0';
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
95 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
96 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
97 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get HTTP minor version.\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
98 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
99 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
100 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
101
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
102 // Get the status code
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
103 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
104 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get status code.\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
105 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
106 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
107 hdr_ptr += 4;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
108
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
109 // Get the reason phrase
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
110 ptr = strstr( hdr_ptr, "\n" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
111 if( hdr_ptr==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
112 mp_msg(MSGT_NETWORK,MSGL_ERR,"Malformed answer. Unable to get the reason phrase.\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
113 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
114 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
115 len = ptr-hdr_ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
116 http_hdr->reason_phrase = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
117 if( http_hdr->reason_phrase==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
118 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
119 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
120 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
121 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
4311
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
122 if( http_hdr->reason_phrase[len-1]=='\r' ) {
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
123 len--;
c9f861653fe2 Modified the output of the http_debug function.
bertrand
parents: 3785
diff changeset
124 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
125 http_hdr->reason_phrase[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
126
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
127 // Set the position of the header separator: \r\n\r\n
8179
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
128 hdr_sep_len = 4;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
129 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
130 if( ptr==NULL ) {
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
131 ptr = strstr( http_hdr->buffer, "\n\n" );
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
132 if( ptr==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
133 mp_msg(MSGT_NETWORK,MSGL_ERR,"Header may be incomplete. No CRLF CRLF found.\n");
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
134 return -1;
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
135 }
8179
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
136 hdr_sep_len = 2;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
137 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
138 pos_hdr_sep = ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
139
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
140 // Point to the first line after the method line.
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
141 hdr_ptr = strstr( http_hdr->buffer, "\n" )+1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
142 do {
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
143 ptr = hdr_ptr;
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
144 while( *ptr!='\r' && *ptr!='\n' ) ptr++;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
145 len = ptr-hdr_ptr;
4816
f1dea39a50bb Fixed the http response parser when the http header only has the HTTP
bertrand
parents: 4311
diff changeset
146 if( len==0 ) break;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
147 field = (char*)realloc(field, len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
148 if( field==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
149 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
150 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
151 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
152 strncpy( field, hdr_ptr, len );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
153 field[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
154 http_set_field( http_hdr, field );
3514
43518985def8 Handle broken server that doesn't send CRLF but jusr LF.
bertrand
parents: 3497
diff changeset
155 hdr_ptr = ptr+((*ptr=='\r')?2:1);
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
156 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
157
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
158 if( field!=NULL ) free( field );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
159
8179
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
160 if( pos_hdr_sep+hdr_sep_len<http_hdr->buffer_size ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
161 // Response has data!
8179
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
162 http_hdr->body = http_hdr->buffer+pos_hdr_sep+hdr_sep_len;
63a5e03f4346 Removed hard coded value for the length of the header separator.
bertrand
parents: 7304
diff changeset
163 http_hdr->body_size = http_hdr->buffer_size-(pos_hdr_sep+hdr_sep_len);
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
164 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
165
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
166 http_hdr->is_parsed = 1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
167 return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
168 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
169
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
170 char *
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
171 http_build_request( HTTP_header_t *http_hdr ) {
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
172 char *ptr, *uri=NULL;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
173 int len;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
174 HTTP_field_t *field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
175 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
176
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
177 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
178 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
179 else {
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12083
diff changeset
180 uri = (char*)malloc(strlen(http_hdr->uri) + 1);
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
181 if( uri==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
182 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
183 return NULL;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
184 }
12391
2677bfac3838 Fix url escaping and avoid double escape
rtognimp
parents: 12083
diff changeset
185 strcpy(uri,http_hdr->uri);
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
186 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
187
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
188 //**** Compute the request length
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
189 // Add the Method line
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
190 len = strlen(http_hdr->method)+strlen(uri)+12;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
191 // Add the fields
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
192 field = http_hdr->first_field;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
193 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
194 len += strlen(field->field_name)+2;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
195 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
196 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
197 // Add the CRLF
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
198 len += 2;
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
199 // Add the body
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
200 if( http_hdr->body!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
201 len += http_hdr->body_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
202 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
203 // Free the buffer if it was previously used
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
204 if( http_hdr->buffer!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
205 free( http_hdr->buffer );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
206 http_hdr->buffer = NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
207 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
208 http_hdr->buffer = (char*)malloc(len+1);
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
209 if( http_hdr->buffer==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
210 mp_msg(MSGT_NETWORK,MSGL_ERR,"Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
211 return NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
212 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
213 http_hdr->buffer_size = len;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
214
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
215 //*** Building the request
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
216 ptr = http_hdr->buffer;
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
217 // Add the method line
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
218 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, uri, http_hdr->http_minor_version );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
219 field = http_hdr->first_field;
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
220 // Add the field
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
221 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
222 ptr += sprintf( ptr, "%s\r\n", field->field_name );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
223 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
224 }
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
225 ptr += sprintf( ptr, "\r\n" );
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
226 // Add the body
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
227 if( http_hdr->body!=NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
228 memcpy( ptr, http_hdr->body, http_hdr->body_size );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
229 }
3497
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
230
6c1e57bdbd96 Made the HTTP request escaped the url.
bertrand
parents: 3039
diff changeset
231 if( uri ) free( uri );
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
232 return http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
233 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
234
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
235 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
236 http_get_field( HTTP_header_t *http_hdr, const char *field_name ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
237 if( http_hdr==NULL || field_name==NULL ) return NULL;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
238 http_hdr->field_search_pos = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
239 http_hdr->field_search = (char*)realloc( http_hdr->field_search, strlen(field_name)+1 );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
240 if( http_hdr->field_search==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
241 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
242 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
243 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
244 strcpy( http_hdr->field_search, field_name );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
245 return http_get_next_field( http_hdr );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
246 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
247
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
248 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
249 http_get_next_field( HTTP_header_t *http_hdr ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
250 char *ptr;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
251 HTTP_field_t *field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
252 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
253
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
254 field = http_hdr->field_search_pos;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
255 while( field!=NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
256 ptr = strstr( field->field_name, ":" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
257 if( ptr==NULL ) return NULL;
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
258 if( !strncasecmp( field->field_name, http_hdr->field_search, ptr-(field->field_name) ) ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
259 ptr++; // Skip the column
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
260 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
261 http_hdr->field_search_pos = field->next;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
262 return ptr; // return the value without the field name
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
263 }
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
264 field = field->next;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
265 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
266 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
267 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
268
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
269 void
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
270 http_set_field( HTTP_header_t *http_hdr, const char *field_name ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
271 HTTP_field_t *new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
272 if( http_hdr==NULL || field_name==NULL ) return;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
273
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
274 new_field = (HTTP_field_t*)malloc(sizeof(HTTP_field_t));
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
275 if( new_field==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
276 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
277 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
278 }
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
279 new_field->next = NULL;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
280 new_field->field_name = (char*)malloc(strlen(field_name)+1);
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
281 if( new_field->field_name==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
282 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
283 return;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
284 }
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
285 strcpy( new_field->field_name, field_name );
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
286
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
287 if( http_hdr->last_field==NULL ) {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
288 http_hdr->first_field = new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
289 } else {
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
290 http_hdr->last_field->next = new_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
291 }
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
292 http_hdr->last_field = new_field;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
293 http_hdr->field_nb++;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
294 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
295
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
296 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
297 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
298 if( http_hdr==NULL || method==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
299
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
300 http_hdr->method = (char*)malloc(strlen(method)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
301 if( http_hdr->method==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
302 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
303 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
304 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
305 strcpy( http_hdr->method, method );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
306 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
307
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
308 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
309 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
310 if( http_hdr==NULL || uri==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
311
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
312 http_hdr->uri = (char*)malloc(strlen(uri)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
313 if( http_hdr->uri==NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
314 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
315 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
316 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
317 strcpy( http_hdr->uri, uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
318 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
319
6514
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
320 int
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
321 http_add_basic_authentication( HTTP_header_t *http_hdr, const char *username, const char *password ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
322 char *auth, *usr_pass, *b64_usr_pass;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
323 int encoded_len, pass_len=0, out_len;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
324 if( http_hdr==NULL || username==NULL ) return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
325
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
326 if( password!=NULL ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
327 pass_len = strlen(password);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
328 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
329
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
330 usr_pass = (char*)malloc(strlen(username)+pass_len+2);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
331 if( usr_pass==NULL ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
332 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
333 return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
334 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
335
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
336 sprintf( usr_pass, "%s:%s", username, (password==NULL)?"":password );
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
337
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
338 // Base 64 encode with at least 33% more data than the original size
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
339 encoded_len = strlen(usr_pass)*2;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
340 b64_usr_pass = (char*)malloc(encoded_len);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
341 if( b64_usr_pass==NULL ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
342 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
343 return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
344 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
345
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
346 out_len = base64_encode( usr_pass, strlen(usr_pass), b64_usr_pass, encoded_len);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
347 if( out_len<0 ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
348 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Base64 out overflow\n");
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
349 return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
350 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
351
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
352 b64_usr_pass[out_len]='\0';
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
353
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
354 auth = (char*)malloc(encoded_len+22);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
355 if( auth==NULL ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
356 mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed\n");
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
357 return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
358 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
359
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
360 sprintf( auth, "Authorization: Basic %s", b64_usr_pass);
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
361 http_set_field( http_hdr, auth );
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
362
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
363 free( usr_pass );
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
364 free( b64_usr_pass );
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
365 free( auth );
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
366
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
367 return 0;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
368 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
369
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
370 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
371 http_debug_hdr( HTTP_header_t *http_hdr ) {
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
372 HTTP_field_t *field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
373 int i = 0;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
374 if( http_hdr==NULL ) return;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
375
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
376 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- START ---\n");
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
377 mp_msg(MSGT_NETWORK,MSGL_V,"protocol: [%s]\n", http_hdr->protocol );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
378 mp_msg(MSGT_NETWORK,MSGL_V,"http minor version: [%d]\n", http_hdr->http_minor_version );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
379 mp_msg(MSGT_NETWORK,MSGL_V,"uri: [%s]\n", http_hdr->uri );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
380 mp_msg(MSGT_NETWORK,MSGL_V,"method: [%s]\n", http_hdr->method );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
381 mp_msg(MSGT_NETWORK,MSGL_V,"status code: [%d]\n", http_hdr->status_code );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
382 mp_msg(MSGT_NETWORK,MSGL_V,"reason phrase: [%s]\n", http_hdr->reason_phrase );
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
383 mp_msg(MSGT_NETWORK,MSGL_V,"body size: [%d]\n", http_hdr->body_size );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
384
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
385 mp_msg(MSGT_NETWORK,MSGL_V,"Fields:\n");
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
386 field = http_hdr->first_field;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
387 while( field!=NULL ) {
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
388 mp_msg(MSGT_NETWORK,MSGL_V," %d - %s\n", i++, field->field_name );
3039
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
389 field = field->next;
80189681c02b Removed the field number limitation.
bertrand
parents: 2489
diff changeset
390 }
5915
f716aa9e2df2 Convert printf to mp_msg in the network layer
albeu
parents: 4816
diff changeset
391 mp_msg(MSGT_NETWORK,MSGL_V,"--- HTTP DEBUG HEADER --- END ---\n");
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
392 }
6514
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
393
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
394 int
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
395 base64_encode(const void *enc, int encLen, char *out, int outMax) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
396 static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
397
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
398 unsigned char *encBuf;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
399 int outLen;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
400 unsigned int bits;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
401 unsigned int shift;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
402
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
403 encBuf = (unsigned char*)enc;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
404 outLen = 0;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
405 bits = 0;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
406 shift = 0;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
407
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
408 while( outLen<outMax ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
409 if( encLen>0 ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
410 // Shift in byte
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
411 bits <<= 8;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
412 bits |= *encBuf;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
413 shift += 8;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
414 // Next byte
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
415 encBuf++;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
416 encLen--;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
417 } else if( shift>0 ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
418 // Pad last bits to 6 bits - will end next loop
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
419 bits <<= 6 - shift;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
420 shift = 6;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
421 } else {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
422 // Terminate with Mime style '='
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
423 *out = '=';
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
424 outLen++;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
425
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
426 return outLen;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
427 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
428
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
429 // Encode 6 bit segments
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
430 while( shift>=6 ) {
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
431 shift -= 6;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
432 *out = b64[ (bits >> shift) & 0x3F ];
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
433 out++;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
434 outLen++;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
435 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
436 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
437
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
438 // Output overflow
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
439 return -1;
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
440 }
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
441
37b0b3302395 Added base64 encoder
bertrand
parents: 6465
diff changeset
442