annotate http.c @ 1485:b895f95e7657

AVI demuxer cleanups, fileformat-dependent stuff moved to priv_t
author arpi
date Sat, 11 Aug 2001 20:37:33 +0000
parents 2803b7076c83
children
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"
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
12
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
13 HTTP_header_t *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
14 http_new_header() {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
15 HTTP_header_t *http_hdr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
16
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
17 http_hdr = (HTTP_header_t*)malloc(sizeof(HTTP_header_t));
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
18 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
19 memset( http_hdr, 0, sizeof(HTTP_header_t) );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
20
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
21 return http_hdr;
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
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
24 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
25 http_free( HTTP_header_t *http_hdr ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
26 int i;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
27 if( http_hdr==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
28 if( http_hdr->protocol!=NULL ) free( http_hdr->protocol );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
29 if( http_hdr->uri!=NULL ) free( http_hdr->uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
30 if( http_hdr->reason_phrase!=NULL ) free( http_hdr->reason_phrase );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
31 if( http_hdr->body!=NULL ) free( http_hdr->body );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
32 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
33 if( http_hdr->method!=NULL ) free( http_hdr->method );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
34 if( http_hdr->buffer!=NULL ) free( http_hdr->buffer );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
35 for( i=0 ; i<http_hdr->field_nb ; i++ )
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
36 if( http_hdr->fields[i]!=NULL ) free( http_hdr->fields[i] );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
37 free( http_hdr );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
38 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
39
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
40 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
41 http_response_append( HTTP_header_t *http_hdr, char *response, int length ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
42 char *ptr = NULL;
1027
2803b7076c83 Checked the length arg when appending data.
bertrand
parents: 902
diff changeset
43 if( http_hdr==NULL || response==NULL || length<0 ) return -1;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
44 ptr = (char*)malloc( http_hdr->buffer_size+length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
45 if( ptr==NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
46 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
47 return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
48 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
49 if( http_hdr->buffer_size==0 ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
50 // Buffer empty, copy response into it.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
51 memcpy( ptr, response, length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
52 http_hdr->buffer_size = length;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
53 } else {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
54 // Buffer not empty, grow buffer, copy and append the response.
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
55 memcpy( ptr, http_hdr->buffer, http_hdr->buffer_size );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
56 free( http_hdr->buffer );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
57 memcpy( ptr+http_hdr->buffer_size, response, length );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
58 http_hdr->buffer_size += length;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
59 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
60 http_hdr->buffer = ptr;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
61 return http_hdr->buffer_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
62 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
63
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
64 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
65 http_is_header_entired( HTTP_header_t *http_hdr ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
66 if( http_hdr==NULL ) return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
67
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
68 if( strstr(http_hdr->buffer, "\r\n\r\n")==NULL ) return 0;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
69 else return 1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
70 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
71
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
72 int
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
73 http_response_parse( HTTP_header_t *http_hdr ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
74 char *hdr_ptr, *ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
75 char *field=NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
76 int pos_hdr_sep, len;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
77 if( http_hdr==NULL ) return -1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
78 if( http_hdr->is_parsed ) return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
79
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
80 // Get the protocol
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
81 hdr_ptr = strstr( http_hdr->buffer, " " );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
82 if( hdr_ptr==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
83 printf("Malformed answer. No space separator found.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
84 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
85 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
86 len = hdr_ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
87 http_hdr->protocol = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
88 if( http_hdr->protocol==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
89 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
90 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
91 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
92 strncpy( http_hdr->protocol, http_hdr->buffer, len );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
93 http_hdr->protocol[len]='\0';
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
94 if( !strncasecmp( http_hdr->protocol, "HTTP", 4) ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
95 if( sscanf( http_hdr->protocol+5,"1.%d", &(http_hdr->http_minor_version) )!=1 ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
96 printf("Malformed answer. Unable to get HTTP minor version.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
97 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
98 }
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 // Get the status code
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
102 if( sscanf( ++hdr_ptr, "%d", &(http_hdr->status_code) )!=1 ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
103 printf("Malformed answer. Unable to get status code.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
104 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
105 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
106 hdr_ptr += 4;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
107
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
108 // Get the reason phrase
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
109 ptr = strstr( hdr_ptr, "\r\n" );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
110 if( hdr_ptr==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
111 printf("Malformed answer. Unable to get the reason phrase.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
112 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
113 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
114 len = ptr-hdr_ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
115 http_hdr->reason_phrase = (char*)malloc(len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
116 if( http_hdr->reason_phrase==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
117 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
118 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
119 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
120 strncpy( http_hdr->reason_phrase, hdr_ptr, len );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
121 http_hdr->reason_phrase[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
122
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
123 // Set the position of the header separator: \r\n\r\n
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
124 ptr = strstr( http_hdr->buffer, "\r\n\r\n" );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
125 if( ptr==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
126 printf("Header may be incomplete. No CRLF CRLF found.\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
127 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
128 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
129 pos_hdr_sep = ptr-http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
130
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
131 hdr_ptr = strstr( http_hdr->buffer, "\r\n" )+2;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
132 do {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
133 ptr = strstr( hdr_ptr, "\r\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
134 if( ptr==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
135 printf("No CRLF found\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
136 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
137 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
138 len = ptr-hdr_ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
139 field = (char*)realloc(field, len+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
140 if( field==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
141 printf("Memory allocation failed\n");
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
142 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
143 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
144 strncpy( field, hdr_ptr, len );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
145 field[len]='\0';
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
146 http_set_field( http_hdr, field );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
147 hdr_ptr = ptr+2;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
148 } while( hdr_ptr<(http_hdr->buffer+pos_hdr_sep) );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
149
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
150 if( field!=NULL ) free( field );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
151
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
152 if( pos_hdr_sep+4<http_hdr->buffer_size ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
153 // Response has data!
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
154 int data_length = http_hdr->buffer_size-(pos_hdr_sep+4);
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
155 http_hdr->body = (char*)malloc( data_length );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
156 if( http_hdr->body==NULL ) {
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
157 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
158 return -1;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
159 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
160 memcpy( http_hdr->body, http_hdr->buffer+pos_hdr_sep+4, data_length );
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
161 http_hdr->body_size = data_length;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
162 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
163
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
164 http_hdr->is_parsed = 1;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
165 return 0;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
166 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
167
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
168 char *
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
169 http_build_request( HTTP_header_t *http_hdr ) {
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
170 char *ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
171 int i;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
172 int len;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
173 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
174
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
175 if( http_hdr->method==NULL ) http_set_method( http_hdr, "GET");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
176 if( http_hdr->uri==NULL ) http_set_uri( http_hdr, "/");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
177
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
178 // Compute the request length
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
179 len = strlen(http_hdr->method)+strlen(http_hdr->uri)+12; // Method line
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
180 for( i=0 ; i<http_hdr->field_nb ; i++ ) // Fields
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
181 len += strlen(http_hdr->fields[i])+2;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
182 len += 2; // CRLF
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
183 if( http_hdr->body!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
184 len += http_hdr->body_size;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
185 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
186 if( http_hdr->buffer!=NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
187 free( http_hdr->buffer );
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
188 http_hdr->buffer = NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
189 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
190 http_hdr->buffer = (char*)malloc(len);
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
191 if( http_hdr->buffer==NULL ) {
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
192 printf("Memory allocation failed\n");
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
193 return NULL;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
194 }
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
195 http_hdr->buffer_size = len;
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
196
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
197 ptr = http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
198 ptr += sprintf( ptr, "%s %s HTTP/1.%d\r\n", http_hdr->method, http_hdr->uri, http_hdr->http_minor_version );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
199 for( i=0 ; i<http_hdr->field_nb ; i++ )
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
200 ptr += sprintf( ptr, "%s\r\n", http_hdr->fields[i] );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
201 ptr += sprintf( ptr, "\r\n" );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
202 if( http_hdr->body!=NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
203 memcpy( ptr, http_hdr->body, http_hdr->body_size );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
204 }
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
205 return http_hdr->buffer;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
206 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
207
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
208 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
209 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
210 if( http_hdr==NULL || field_name==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
211 http_hdr->search_pos = 0;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
212 if( http_hdr->field_search!=NULL ) free( http_hdr->field_search );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
213 http_hdr->field_search = (char*)malloc(strlen(field_name)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
214 if( http_hdr->field_search==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
215 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
216 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
217 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
218 strcpy( http_hdr->field_search, field_name );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
219 return http_get_next_field( http_hdr );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
220 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
221
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
222 char *
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
223 http_get_next_field( HTTP_header_t *http_hdr ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
224 char *ptr;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
225 int i;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
226 if( http_hdr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
227
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
228 for( i=http_hdr->search_pos ; i<http_hdr->field_nb ; i++ ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
229 ptr = strstr( http_hdr->fields[i], ":" );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
230 if( ptr==NULL ) return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
231 if( !strncasecmp( http_hdr->fields[i], http_hdr->field_search, ptr-http_hdr->fields[i] ) ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
232 ptr++; // Skip the column
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
233 while( ptr[0]==' ' ) ptr++; // Skip the spaces if there is some
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
234 http_hdr->search_pos = i+1;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
235 return ptr; // return the value without the field name
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
236 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
237 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
238 return NULL;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
239 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
240
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
241 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
242 http_set_field( HTTP_header_t *http_hdr, const char *field ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
243 int pos;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
244 if( http_hdr==NULL || field==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
245
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
246 pos = http_hdr->field_nb;
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 http_hdr->fields[pos] = (char*)malloc(strlen(field)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
249 if( http_hdr->fields[pos]==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
250 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
251 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
252 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
253 http_hdr->field_nb++;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
254 strcpy( http_hdr->fields[pos], field );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
255 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
256
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
257 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
258 http_set_method( HTTP_header_t *http_hdr, const char *method ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
259 if( http_hdr==NULL || method==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
260
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
261 http_hdr->method = (char*)malloc(strlen(method)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
262 if( http_hdr->method==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
263 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
264 return;
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 strcpy( http_hdr->method, method );
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
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
270 http_set_uri( HTTP_header_t *http_hdr, const char *uri ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
271 if( http_hdr==NULL || uri==NULL ) return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
272
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
273 http_hdr->uri = (char*)malloc(strlen(uri)+1);
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
274 if( http_hdr->uri==NULL ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
275 printf("Memory allocation failed\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
276 return;
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
277 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
278 strcpy( http_hdr->uri, uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
279 }
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
280
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
281 void
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
282 http_debug_hdr( HTTP_header_t *http_hdr ) {
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
283 int i;
902
ede5785faa53 Bugs fix, improvements...
bertrand
parents: 870
diff changeset
284 if( http_hdr==NULL ) return;
870
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
285
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
286 printf("protocol: %s\n", http_hdr->protocol );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
287 printf("http minor version: %d\n", http_hdr->http_minor_version );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
288 printf("uri: %s\n", http_hdr->uri );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
289 printf("method: %s\n", http_hdr->method );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
290 printf("status code: %d\n", http_hdr->status_code );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
291 printf("reason phrase: %s\n", http_hdr->reason_phrase );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
292
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
293 printf("Fields:\n");
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
294 for( i=0 ; i<http_hdr->field_nb ; i++ )
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
295 printf(" %d - %s\n", i, http_hdr->fields[i] );
f641c96e431b Some simple code to handle HTTP requests/responses.
bertrand
parents:
diff changeset
296 }