summaryrefslogtreecommitdiff
path: root/ext/soap/php_http.c
blob: 1ebb59d3cbae1a4135b6c3b097a73e63735b4783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "php_soap.h"

void send_http_soap_request(zval *this_ptr, xmlDoc *doc, char *function_name, char *soapaction TSRMLS_DC)
{
	xmlChar *buf;
	char *soap_headers;
	int buf_size,err,ret;
	sdlPtr sdl;
	php_url *phpurl = NULL;
	SOAP_STREAM stream;
	zval **trace;

/*	TSRMLS_FETCH();*/

	FETCH_THIS_SOCKET(stream);
	FETCH_THIS_URL(phpurl);
	FETCH_THIS_SDL(sdl);

	xmlDocDumpMemory(doc, &buf, &buf_size);

	if(!buf)
		php_error(E_ERROR, "Error build soap request");

	if(zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS
		&& Z_LVAL_PP(trace) > 0)
		add_property_stringl(this_ptr, "__last_request", buf, buf_size, 1);

	if(!stream)
	{
		char *url;

		if(!sdl)
		{
			zval **location;
			if(zend_hash_find(Z_OBJPROP_P(this_ptr), "location", sizeof("location"),(void **) &location) == FAILURE)
				php_error(E_ERROR, "Error could not find location");
			url = Z_STRVAL_PP(location);
		}
		else
		{
			sdlBindingPtr binding;
			FETCH_THIS_PORT(binding);
			url = binding->location;
		}

		phpurl = php_url_parse(url);

#ifdef PHP_STREAMS
		stream = php_stream_sock_open_host(phpurl->host, (unsigned short)(phpurl->port == 0 ? 80 : phpurl->port), SOCK_STREAM, 0, 0);
#else
		stream = get_socket(phpurl->host, (phpurl->port == 0 ? 80 : phpurl->port), 10);
#endif
		if(stream)
		{
			ret = zend_list_insert((void *)stream, le_http_socket);
			add_property_resource(this_ptr, "httpsocket", ret);
			zend_list_addref(ret);

			ret = zend_list_insert(phpurl, le_url);
			add_property_resource(this_ptr, "httpurl", ret);
			zend_list_addref(ret);
		}
		else
			php_error(E_ERROR,"Could not connect to host");
		/*
		 php_url_free(phpurl);
		 */
	}

	if(stream)
	{
		zval **cookies;
		char *header = "POST %s HTTP/1.1\r\nConnection: close\r\nAccept: text/html; text/xml; text/plain\r\nUser-Agent: PHP SOAP 0.1\r\nHost: %s\r\nContent-Type: text/xml\r\nContent-Length: %d\r\nSOAPAction: \"%s\"\r\n";
		int size = strlen(header) + strlen(phpurl->host) + strlen(phpurl->path) + 10;

		/* TODO: Add authentication */
		if(sdl != NULL)
		{
			/* TODO: need to grab soap action from wsdl....*/
			soap_headers = emalloc(size + strlen(soapaction));
			sprintf(soap_headers, header, phpurl->path, phpurl->host, buf_size, soapaction);
		}
		else
		{
			soap_headers = emalloc(size + strlen(soapaction));
			sprintf(soap_headers, header, phpurl->path, phpurl->host, buf_size, soapaction);
		}

#ifdef PHP_STREAMS
		err = php_stream_write(stream, soap_headers, strlen(soap_headers));
#else
		err = send(stream, soap_headers, strlen(soap_headers), 0);
#endif
		if(err != (int)strlen(soap_headers))
			php_error(E_ERROR,"Failed Sending HTTP Headers");

		/* Send cookies along with request */
		if(zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == SUCCESS)
		{
			smart_str cookie_str = {0};
			zval **data;
			char *key;
			int index, i;

			zend_hash_internal_pointer_reset(Z_ARRVAL_PP(cookies));
			for(i = 0;i < (int)Z_ARRVAL_PP(cookies)->nNumOfElements;i++)
			{
				zend_hash_get_current_data(Z_ARRVAL_PP(cookies), (void **)&data);
				zend_hash_get_current_key(Z_ARRVAL_PP(cookies), &key, (long *)&index, FALSE);

				smart_str_appendl(&cookie_str, "Cookie: ", 8);
				smart_str_appendl(&cookie_str, key, strlen(key));
				smart_str_appendc(&cookie_str, '=');
				smart_str_appendl(&cookie_str, Z_STRVAL_PP(data), Z_STRLEN_PP(data));
				smart_str_appendc(&cookie_str, ';');
				zend_hash_move_forward(Z_ARRVAL_PP(cookies));
			}
			smart_str_appendl(&cookie_str, "\r\n", 2);
			smart_str_0(&cookie_str);

#ifdef PHP_STREAMS
			err = php_stream_write(stream, cookie_str.c, cookie_str.len);
#else
			err = send(stream, cookie_str.c, cookie_str.len,0);
#endif
			if(err != (int)cookie_str.len)
				php_error(E_ERROR,"Failed Sending HTTP Headers");

			smart_str_free(&cookie_str);
		}

#ifdef PHP_STREAMS
		err = php_stream_write(stream, "\r\n", 2);
#else
		err = send(stream, "\r\n", 2, 0);
#endif
		if(err != 2)
			php_error(E_ERROR,"Failed Sending HTTP Headers");


#ifdef PHP_STREAMS
		err = php_stream_write(stream, buf, buf_size);
#else
		err = send(stream, buf, buf_size, 0);
#endif
		if(err != (int)strlen(buf))
			php_error(E_ERROR,"Failed Sending HTTP Content");

		efree(soap_headers);
	}
	xmlFree(buf);
}

void get_http_soap_response(zval *this_ptr, char **buffer, int *buffer_len TSRMLS_DC)
{
	char *http_headers, *http_body, *content_type, *http_version, http_status[4], *cookie_itt;
	int http_header_size, http_body_size, http_close;
	sdlPtr sdl;
	zval **socket_ref;
	SOAP_STREAM stream;
	zval **trace;

/*	TSRMLS_FETCH();*/

	FETCH_THIS_SDL(sdl);

	if(FIND_SOCKET_PROPERTY(this_ptr, socket_ref) != FAILURE)
	{
		FETCH_SOCKET_RES(stream, socket_ref);
	}

	if(!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC))
		php_error(E_ERROR, "Error Fetching http headers");

	/* Check to see what HTTP status was sent */
	http_version = get_http_header_value(http_headers,"HTTP/");
	if(http_version)
	{
		char *tmp;

		tmp = strstr(http_version," ");

		if(tmp != NULL)
		{
			tmp++;
			strncpy(http_status,tmp,4);
			http_status[3] = '\0';
		}

		/*
		Try and process any respsone that is xml might contain fault code

		Maybe try and test for some of the 300's 400's specfics but not
		right now.

		if(strcmp(http_status,"200"))
		{
			zval *err;
			char *http_err;

			MAKE_STD_ZVAL(err);
			ZVAL_STRING(err, http_body, 1);
			http_err = emalloc(strlen("HTTP request failed ()") + 4);
			sprintf(http_err, "HTTP request failed (%s)", http_status);
			add_soap_fault(thisObj, "SOAP-ENV:Client", http_err, NULL, err TSRMLS_CC);
			efree(http_err);
			return;
		}*/

		/* Try and get headers again */
		if(!strcmp(http_status, "100"))
		{
			if(!get_http_headers(stream, &http_headers, &http_header_size TSRMLS_CC))
				php_error(E_ERROR, "Error Fetching http headers");
		}

		efree(http_version);
	}
	
	if(!get_http_body(stream, http_headers, &http_body, &http_body_size TSRMLS_CC))
		php_error(E_ERROR, "Error Fetching http body");

	if(zend_hash_find(Z_OBJPROP_P(this_ptr), "trace", sizeof("trace"), (void **) &trace) == SUCCESS
		&& Z_LVAL_PP(trace) > 0)
		add_property_stringl(this_ptr, "__last_response", http_body, http_body_size, 1);

	/* Close every time right now till i can spend more time on it
	   it works.. it's just slower??
	*/
	/* See if the server requested a close */
	http_close = TRUE;
	/*
	connection = get_http_header_value(http_headers,"Connection: ");
	if(connection)
	{
		if(!strcmp(connection, "Keep-Alive"))
			http_close = FALSE;
		efree(connection);
	}
	else
	{
		if(!strncmp(http_version,"1.1", 3))
				http_close = FALSE;
	}
	*/

	if(http_close)
	{
#ifdef PHP_STREAMS
		php_stream_close(stream);
#else
		SOCK_CLOSE(stream);
#endif
		zend_list_delete(Z_RESVAL_PP(socket_ref));
		zend_hash_del(Z_OBJPROP_P(this_ptr), "httpsocket", strlen("httpsocket") + 1);
	}

	/* Check and see if the server even sent a xml document */
	content_type = get_http_header_value(http_headers,"Content-Type: ");
	if(content_type)
	{
		char *pos = NULL;
		int cmplen;
		pos = strstr(content_type,";");
		if(pos != NULL)
			cmplen = pos - content_type;
		else
			cmplen = strlen(content_type);

		if(strncmp(content_type, "text/xml", cmplen))
		{
			if(strncmp(http_body, "<?xml", 5))
			{
				zval *err;
				MAKE_STD_ZVAL(err);
				ZVAL_STRINGL(err, http_body, http_body_size, 1);
				add_soap_fault(this_ptr, "SOAP-ENV:Client", "Didn't recieve an xml document", NULL, err TSRMLS_CC);
				efree(content_type);
				return;
			}
		}
		efree(content_type);
	}

	/* Grab and send back every cookie */
	
	/* Not going to worry about Path: because
	   we shouldn't be changing urls so path dont
	   matter too much
	*/
	cookie_itt = strstr(http_headers,"Set-Cookie: ");
	while(cookie_itt)
	{
		char *end_pos, *cookie;
		char *eqpos, *sempos;
		smart_str name = {0}, value = {0};
		zval **cookies, *z_cookie;

		if(zend_hash_find(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), (void **)&cookies) == FAILURE)
		{
			zval *tmp_cookies;
			MAKE_STD_ZVAL(tmp_cookies);
			array_init(tmp_cookies);
			zend_hash_update(Z_OBJPROP_P(this_ptr), "_cookies", sizeof("_cookies"), &tmp_cookies, sizeof(zval *), (void **)&cookies);
		}

		end_pos = strstr(cookie_itt,"\r\n");
		cookie = get_http_header_value(cookie_itt,"Set-Cookie: ");

		eqpos = strstr(cookie, "=");
		sempos = strstr(cookie, ";");

		smart_str_appendl(&name, cookie, eqpos - cookie);
		smart_str_0(&name);

		smart_str_appendl(&value, eqpos + 1, sempos - (eqpos + 1));
		smart_str_0(&value);

		MAKE_STD_ZVAL(z_cookie);
		ZVAL_STRINGL(z_cookie, value.c, value.len, 1);

		zend_hash_update(Z_ARRVAL_PP(cookies), name.c, name.len + 1, &z_cookie, sizeof(zval *), NULL);

		cookie_itt = strstr(cookie_itt + sizeof("Set-Cookie: "), "Set-Cookie: ");

		smart_str_free(&value);
		smart_str_free(&name);
		efree(cookie);
		cookie_itt = FALSE;
	}

	*buffer = http_body;
	*buffer_len = http_body_size;
	efree(http_headers);
}

char *get_http_header_value(char *headers, char *type)
{
	char *tmp = NULL,*var = NULL;
	int size;

	tmp = strstr(headers, type);
	if(tmp != NULL)
	{
		tmp += strlen(type);
		size = strstr(tmp, "\r\n") - tmp;
		var = emalloc(size + 1);
		strncpy(var, tmp, size);
		var[size] = '\0';
	}
	return var;
}

int get_http_body(SOAP_STREAM stream, char *headers,  char **response, int *out_size TSRMLS_DC)
{
	char *trans_enc, *content_length, *http_buf;
	int http_buf_size = 0;
/*	TSRMLS_FETCH();*/

	trans_enc = get_http_header_value(headers, "Transfer-Encoding: ");
	content_length = get_http_header_value(headers, "Content-Length: ");

	/* this is temp...
	  netscape enterprise server sends in lowercase???
	*/
	if(content_length == NULL)
		content_length = get_http_header_value(headers, "Content-length: ");

	if(trans_enc && !strcmp(trans_enc, "chunked"))
	{
		int cur = 0, size = 0, buf_size = 0, len_size;
		char done, chunk_size[10];

		done = FALSE;
		http_buf = emalloc(1);
		*http_buf = '\0';
		while(!done)
		{
			for (cur = 0; cur < 3 || !(chunk_size[cur - 2] == '\r' && chunk_size[cur - 1] == '\n'); cur++)
#ifdef PHP_STREAMS
				chunk_size[cur] = php_stream_getc(stream);
#else
				chunk_size[cur] = php_sock_fgetc(stream);
#endif
			chunk_size[cur] = '\0';
			if(sscanf(chunk_size,"%x",&buf_size) != -1)
			{
				http_buf = erealloc(http_buf,http_buf_size + buf_size);
				len_size = 0;
				while(http_buf_size < buf_size)
				{
#ifdef PHP_STREAMS
					len_size += php_stream_read(stream, &http_buf[http_buf_size], buf_size - len_size);
#else
					len_size += php_sock_fread(&http_buf[http_buf_size], buf_size - len_size, stream);
#endif
					http_buf_size += len_size;
				}
#ifdef PHP_STREAMS
				php_stream_getc(stream);php_stream_getc(stream);
#else
				/* Eat up '\r' '\n' */
				php_sock_fgetc(stream);php_sock_fgetc(stream);
#endif
			}
			if(buf_size == 0)
				done = TRUE;
		}
		efree(trans_enc);
	}
	else if(content_length)
	{
		int size;
		size = atoi(content_length);
		http_buf = emalloc(size + 1);

		while(http_buf_size < size)
#ifdef PHP_STREAMS
			http_buf_size += php_stream_read(stream, &http_buf[http_buf_size], size - http_buf_size);
#else
			http_buf_size += php_sock_fread(&http_buf[http_buf_size], size - http_buf_size, stream);
#endif
		http_buf[size] = '\0';
		efree(content_length);
	}
	else
		php_error(E_ERROR,"Don't know how to read http body, No Content-Length or chunked data");

	(*response) = http_buf;
	(*out_size) = http_buf_size;
	return TRUE;
}

int get_http_headers(SOAP_STREAM stream, char **response, int *out_size TSRMLS_DC)
{
	int done;
	char chr;
	smart_str tmp_response = {0};
/*	TSRMLS_FETCH();//i think this is not needed - even the parameter */

	done = FALSE;

	while(!done)
	{
#ifdef PHP_STREAMS
		chr = php_stream_getc(stream);
#else
		chr = php_sock_fgetc(stream);
#endif
		if(chr != EOF)
		{
			smart_str_appendc(&tmp_response, chr);
			if(tmp_response.c[tmp_response.len - 2] == '\r' && tmp_response.c[tmp_response.len - 1] == '\n' &&
				tmp_response.c[tmp_response.len - 4] == '\r' && tmp_response.c[tmp_response.len - 3] == '\n')
			{
				smart_str_0(&tmp_response);
				done = TRUE;
			}
		}
		else
			return FALSE;
	}
	(*response) = tmp_response.c;
	(*out_size) = tmp_response.len;
	return TRUE;
}

#ifndef PHP_STREAMS
SOCKET get_socket(char* host,int portno,int time)
{
	SOCKET socketd = -1;
	struct sockaddr_in server;
	struct timeval timeout;

	memset(&server, 0, sizeof(server));
	socketd = socket(AF_INET,SOCK_STREAM,0);
	if (socketd == SOCK_ERR) {
		if(socketd > 0)
			SOCK_CLOSE(socketd);
		return FALSE;
	}
	server.sin_family = AF_INET;

	if(php_lookup_hostname(host,&server.sin_addr)) {
		if(socketd > 0)
			SOCK_CLOSE(socketd);
		return FALSE;
	}
	server.sin_port = htons((unsigned short)portno);
	timeout.tv_sec = time;
	timeout.tv_usec = 0;
	if (php_connect_nonb(socketd, (struct sockaddr *)&server, sizeof(struct sockaddr_in), &timeout) == SOCK_CONN_ERR) {
		if(socketd > 0)
			SOCK_CLOSE(socketd);
		return FALSE;
	}

	return socketd;
}
#endif