722e0b5a3ddd8b7e46d8eaeac0aed20deb757cb6
[m6w6/ext-http] / http_request_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #endif
25
26 #include <curl/curl.h>
27
28 #include "phpstr/phpstr.h"
29
30 #include "php.h"
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_requestpool_object.h"
37 #include "php_http_url_api.h"
38
39 #ifndef HTTP_CURL_USE_ZEND_MM
40 # define HTTP_CURL_USE_ZEND_MM 0
41 #endif
42
43 #ifndef HTTP_DEBUG_REQPOOLS
44 # define HTTP_DEBUG_REQPOOLS 0
45 #endif
46
47 ZEND_EXTERN_MODULE_GLOBALS(http)
48
49 #if LIBCURL_VERSION_NUM < 0x070c00
50 # define curl_easy_strerror(code) HTTP_G(request).error
51 #endif
52
53 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
54 #define HTTP_CURL_INFO_EX(I, X) \
55 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
56 { \
57 case CURLINFO_STRING: \
58 { \
59 char *c; \
60 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
61 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
62 } \
63 } \
64 break; \
65 \
66 case CURLINFO_DOUBLE: \
67 { \
68 double d; \
69 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
70 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
71 } \
72 } \
73 break; \
74 \
75 case CURLINFO_LONG: \
76 { \
77 long l; \
78 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
79 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
80 } \
81 } \
82 break; \
83 }
84
85 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
86 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
87 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
88 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
89 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
90 if (!strcasecmp(key, #keyname)) { \
91 convert_to_string_ex(param); \
92 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
93 key = NULL; \
94 continue; \
95 }
96 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
97 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
98 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
99 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
100 if (!strcasecmp(key, #keyname)) { \
101 convert_to_long_ex(param); \
102 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
103 key = NULL; \
104 continue; \
105 }
106
107
108 static const char *const http_request_methods[HTTP_MAX_REQUEST_METHOD + 1];
109 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
110 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
111 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
112 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
113 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
114 static int http_curl_progress_callback(void *, double, double, double, double);
115 static int http_curl_debug_callback(CURL *, curl_infotype, char *, size_t, void *);
116
117 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
118 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
119 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
120 type (var) = (type) (__CTX->data)
121
122 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
123 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC);
124
125 static void http_request_pool_freebody(http_request_body **body);
126 static void http_request_pool_freehandle(zval **request, http_request_pool *pool TSRMLS_DC);
127
128 #if HTTP_CURL_USE_ZEND_MM
129 static void http_curl_free(void *p) { efree(p); }
130 static char *http_curl_strdup(const char *p) { return estrdup(p); }
131 static void *http_curl_malloc(size_t s) { return emalloc(s); }
132 static void *http_curl_realloc(void *p, size_t s) { return erealloc(p, s); }
133 static void *http_curl_calloc(size_t n, size_t s) { return ecalloc(n, s); }
134 #endif
135
136 /* {{{ STATUS http_request_global_init() */
137 STATUS _http_request_global_init(void)
138 {
139 #if HTTP_CURL_USE_ZEND_MM
140 if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
141 http_curl_malloc,
142 http_curl_free,
143 http_curl_realloc,
144 http_curl_strdup,
145 http_curl_calloc)) {
146 return FAILURE;
147 }
148 #else
149 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
150 return FAILURE;
151 }
152 #endif
153 return SUCCESS;
154 }
155 /* }}} */
156
157 /* {{{ void *http_request_data_copy(int, void *) */
158 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
159 {
160 switch (type)
161 {
162 case COPY_STRING:
163 {
164 char *new_str = estrdup((const char*) data);
165 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
166 return new_str;
167 }
168
169 case COPY_SLIST:
170 {
171 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
172 return data;
173 }
174
175 case COPY_CONTEXT:
176 {
177 zend_llist_add_element(&HTTP_G(request).copies.contexts, &data);
178 return data;
179 }
180
181 default:
182 {
183 return data;
184 }
185 }
186 }
187 /* }}} */
188
189 /* {{{ void http_request_data_free_string(char **) */
190 void _http_request_data_free_string(void *string)
191 {
192 efree(*((char **)string));
193 }
194 /* }}} */
195
196 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
197 void _http_request_data_free_slist(void *list)
198 {
199 curl_slist_free_all(*((struct curl_slist **) list));
200 }
201 /* }}} */
202
203 /* {{{ _http_request_data_free_context(http_curl_callback_ctx **) */
204 void _http_request_data_free_context(void *context)
205 {
206 efree(*((http_curl_callback_ctx **) context));
207 }
208 /* }}} */
209
210 /* {{{ http_request_body *http_request_body_new() */
211 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
212 {
213 http_request_body *body = ecalloc(1, sizeof(http_request_body));
214 return body;
215 }
216 /* }}} */
217
218 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
219 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
220 {
221 if (files && (zend_hash_num_elements(files) > 0)) {
222 char *key = NULL;
223 ulong idx;
224 zval **data;
225 struct curl_httppost *http_post_data[2] = {NULL, NULL};
226
227 /* normal data */
228 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
229 CURLcode err;
230 if (key) {
231 convert_to_string_ex(data);
232 err = curl_formadd(&http_post_data[0], &http_post_data[1],
233 CURLFORM_COPYNAME, key,
234 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
235 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
236 CURLFORM_END
237 );
238 if (CURLE_OK != err) {
239 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post fields: %s", curl_easy_strerror(err));
240 curl_formfree(http_post_data[0]);
241 return FAILURE;
242 }
243
244 /* reset */
245 key = NULL;
246 }
247 }
248
249 /* file data */
250 FOREACH_HASH_VAL(files, data) {
251 CURLcode err;
252 zval **file, **type, **name;
253 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
254 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
255 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
256 err = curl_formadd(&http_post_data[0], &http_post_data[1],
257 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
258 CURLFORM_FILE, Z_STRVAL_PP(file),
259 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
260 CURLFORM_END
261 );
262 if (CURLE_OK != err) {
263 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post files: %s", curl_easy_strerror(err));
264 curl_formfree(http_post_data[0]);
265 return FAILURE;
266 }
267 } else {
268 http_error(E_NOTICE, HTTP_E_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
269 }
270 }
271
272 body->type = HTTP_REQUEST_BODY_CURLPOST;
273 body->data = http_post_data[0];
274 body->size = 0;
275
276 } else {
277 char *encoded;
278 size_t encoded_len;
279
280 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
281 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
282 return FAILURE;
283 }
284
285 body->type = HTTP_REQUEST_BODY_CSTRING;
286 body->data = encoded;
287 body->size = encoded_len;
288 }
289
290 return SUCCESS;
291 }
292 /* }}} */
293
294 /* {{{ void http_request_body_dtor(http_request_body *) */
295 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
296 {
297 if (body) {
298 switch (body->type)
299 {
300 case HTTP_REQUEST_BODY_CSTRING:
301 if (body->data) {
302 efree(body->data);
303 }
304 break;
305
306 case HTTP_REQUEST_BODY_CURLPOST:
307 curl_formfree(body->data);
308 break;
309
310 case HTTP_REQUEST_BODY_UPLOADFILE:
311 php_stream_close(body->data);
312 break;
313 }
314 }
315 }
316 /* }}} */
317
318 /* {{{ void http_request_body_free(http_request_body *) */
319 PHP_HTTP_API void _http_request_body_free(http_request_body *body TSRMLS_DC)
320 {
321 if (body) {
322 http_request_body_dtor(body);
323 efree(body);
324 }
325 }
326 /* }}} */
327
328 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
329 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, phpstr *response TSRMLS_DC)
330 {
331 zval *zoption;
332 zend_bool range_req = 0;
333
334 /* reset CURL handle */
335 #if LIBCURL_VERSION_NUM >= 0x070c01
336 curl_easy_reset(ch);
337 #endif
338
339 /* set options */
340 if (url) {
341 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, (void *) url));
342 }
343
344 if (response) {
345 HTTP_CURL_OPT(WRITEDATA, http_curl_callback_data(response));
346 HTTP_CURL_OPT(WRITEHEADER, http_curl_callback_data(response));
347 }
348
349 HTTP_CURL_OPT(HEADER, 0);
350 HTTP_CURL_OPT(FILETIME, 1);
351 HTTP_CURL_OPT(AUTOREFERER, 1);
352 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
353 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_write_callback);
354 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_write_callback);
355
356 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
357 HTTP_CURL_OPT(NOSIGNAL, 1);
358 #endif
359 #if LIBCURL_VERSION_NUM < 0x070c00
360 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).error);
361 #endif
362
363 /* progress callback */
364 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
365 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
366 HTTP_CURL_OPT(PROGRESSDATA, http_curl_callback_data(zoption));
367 } else {
368 HTTP_CURL_OPT(NOPROGRESS, 1);
369 }
370
371 /* debug callback */
372 if (zoption = http_curl_getopt(options, "ondebug", 0)) {
373 HTTP_CURL_OPT(VERBOSE, 1);
374 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_debug_callback);
375 HTTP_CURL_OPT(DEBUGDATA, http_curl_callback_data(zoption));
376 } else {
377 HTTP_CURL_OPT(VERBOSE, 0);
378 }
379
380 /* proxy */
381 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
382 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
383 /* port */
384 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
385 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
386 }
387 /* user:pass */
388 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
389 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
390 }
391 #if LIBCURL_VERSION_NUM >= 0x070a07
392 /* auth method */
393 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
394 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
395 }
396 #endif
397 }
398
399 /* outgoing interface */
400 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
401 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
402 }
403
404 /* another port */
405 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
406 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
407 }
408
409 /* auth */
410 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
411 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
412 }
413 #if LIBCURL_VERSION_NUM >= 0x070a06
414 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
415 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
416 }
417 #endif
418
419 /* compress, empty string enables deflate and gzip */
420 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
421 if (Z_LVAL_P(zoption)) {
422 HTTP_CURL_OPT(ENCODING, http_request_data_copy(COPY_STRING, ""));
423 }
424 }
425
426 /* redirects, defaults to 0 */
427 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
428 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
429 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
430 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
431 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
432 }
433 } else {
434 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
435 }
436
437 /* referer */
438 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
439 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
440 }
441
442 /* useragent, default "PECL::HTTP/version (PHP/version)" */
443 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
444 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
445 } else {
446 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")"));
447 }
448
449 /* additional headers, array('name' => 'value') */
450 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
451 char *header_key;
452 ulong header_idx;
453 struct curl_slist *headers = NULL;
454
455 FOREACH_KEY(zoption, header_key, header_idx) {
456 if (header_key) {
457 zval **header_val;
458 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
459 char header[1024] = {0};
460 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
461 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
462 }
463
464 /* reset */
465 header_key = NULL;
466 }
467 }
468
469 if (headers) {
470 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
471 }
472 } else {
473 HTTP_CURL_OPT(HTTPHEADER, NULL);
474 }
475
476 /* cookies, array('name' => 'value') */
477 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
478 char *cookie_key = NULL;
479 ulong cookie_idx = 0;
480 phpstr *qstr = phpstr_new();
481
482 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
483 if (cookie_key) {
484 zval **cookie_val;
485 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
486 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
487 }
488
489 /* reset */
490 cookie_key = NULL;
491 }
492 }
493
494 if (qstr->used) {
495 phpstr_fix(qstr);
496 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
497 }
498 phpstr_free(qstr);
499 }
500
501 /* cookiestore */
502 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
503 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
504 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
505 }
506
507 /* resume */
508 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
509 range_req = 1;
510 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
511 }
512
513 /* maxfilesize */
514 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
515 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
516 }
517
518 /* lastmodified */
519 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
520 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
521 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
522 }
523
524 /* timeout */
525 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
526 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
527 }
528
529 /* connecttimeout, defaults to 1 */
530 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
531 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
532 } else {
533 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
534 }
535
536 /* ssl */
537 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
538 ulong idx;
539 char *key = NULL;
540 zval **param;
541
542 FOREACH_KEYVAL(zoption, key, idx, param) {
543 if (key) {
544 HTTP_CURL_OPT_SSL_STRING(CERT);
545 #if LIBCURL_VERSION_NUM >= 0x070903
546 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
547 #endif
548 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
549
550 HTTP_CURL_OPT_SSL_STRING(KEY);
551 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
552 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
553
554 HTTP_CURL_OPT_SSL_STRING(ENGINE);
555 HTTP_CURL_OPT_SSL_LONG(VERSION);
556
557 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
558 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
559 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
560
561
562 HTTP_CURL_OPT_STRING(CAINFO);
563 #if LIBCURL_VERSION_NUM >= 0x070908
564 HTTP_CURL_OPT_STRING(CAPATH);
565 #endif
566 HTTP_CURL_OPT_STRING(RANDOM_FILE);
567 HTTP_CURL_OPT_STRING(EGDSOCKET);
568
569 /* reset key */
570 key = NULL;
571 }
572 }
573 } else {
574 /* disable SSL verification by default */
575 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
576 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
577 }
578
579 /* request method */
580 switch (meth)
581 {
582 case HTTP_GET:
583 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
584 break;
585
586 case HTTP_HEAD:
587 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
588 break;
589
590 case HTTP_POST:
591 curl_easy_setopt(ch, CURLOPT_POST, 1);
592 break;
593
594 case HTTP_PUT:
595 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
596 break;
597
598 default:
599 if (http_request_method_exists(0, meth, NULL)) {
600 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
601 } else {
602 http_error_ex(E_WARNING, HTTP_E_CURL, "Unsupported request method: %d", meth);
603 return FAILURE;
604 }
605 break;
606 }
607
608 /* attach request body */
609 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
610 switch (body->type)
611 {
612 case HTTP_REQUEST_BODY_CSTRING:
613 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, body->data);
614 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
615 break;
616
617 case HTTP_REQUEST_BODY_CURLPOST:
618 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
619 break;
620
621 case HTTP_REQUEST_BODY_UPLOADFILE:
622 curl_easy_setopt(ch, CURLOPT_READDATA, http_curl_callback_data(body));
623 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
624 break;
625
626 default:
627 http_error_ex(E_WARNING, HTTP_E_CURL, "Unknown request body type: %d", body->type);
628 return FAILURE;
629 break;
630 }
631 }
632
633 return SUCCESS;
634 }
635 /* }}} */
636
637 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
638 PHP_HTTP_API STATUS _http_request_exec(CURL *ch, HashTable *info TSRMLS_DC)
639 {
640 CURLcode result;
641
642 /* perform request */
643 if (CURLE_OK != (result = curl_easy_perform(ch))) {
644 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result));
645 return FAILURE;
646 } else {
647 /* get curl info */
648 if (info) {
649 http_request_info(ch, info);
650 }
651 return SUCCESS;
652 }
653 }
654 /* }}} */
655
656 /* {{{ void http_request_info(CURL *, HashTable *) */
657 PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
658 {
659 zval array;
660 Z_ARRVAL(array) = info;
661
662 HTTP_CURL_INFO(EFFECTIVE_URL);
663 #if LIBCURL_VERSION_NUM >= 0x070a07
664 HTTP_CURL_INFO(RESPONSE_CODE);
665 #else
666 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
667 #endif
668 HTTP_CURL_INFO(HTTP_CONNECTCODE);
669 #if LIBCURL_VERSION_NUM >= 0x070500
670 HTTP_CURL_INFO(FILETIME);
671 #endif
672 HTTP_CURL_INFO(TOTAL_TIME);
673 HTTP_CURL_INFO(NAMELOOKUP_TIME);
674 HTTP_CURL_INFO(CONNECT_TIME);
675 HTTP_CURL_INFO(PRETRANSFER_TIME);
676 HTTP_CURL_INFO(STARTTRANSFER_TIME);
677 #if LIBCURL_VERSION_NUM >= 0x070907
678 HTTP_CURL_INFO(REDIRECT_TIME);
679 HTTP_CURL_INFO(REDIRECT_COUNT);
680 #endif
681 HTTP_CURL_INFO(SIZE_UPLOAD);
682 HTTP_CURL_INFO(SIZE_DOWNLOAD);
683 HTTP_CURL_INFO(SPEED_DOWNLOAD);
684 HTTP_CURL_INFO(SPEED_UPLOAD);
685 HTTP_CURL_INFO(HEADER_SIZE);
686 HTTP_CURL_INFO(REQUEST_SIZE);
687 HTTP_CURL_INFO(SSL_VERIFYRESULT);
688 #if LIBCURL_VERSION_NUM >= 0x070c03
689 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
690 #endif
691 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
692 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
693 HTTP_CURL_INFO(CONTENT_TYPE);
694 #if LIBCURL_VERSION_NUM >= 0x070a03
695 /*HTTP_CURL_INFO(PRIVATE);*/
696 #endif
697 #if LIBCURL_VERSION_NUM >= 0x070a08
698 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
699 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
700 #endif
701 #if LIBCURL_VERSION_NUM >= 0x070c02
702 /*HTTP_CURL_INFO(OS_ERRNO);*/
703 #endif
704 #if LIBCURL_VERSION_NUM >= 0x070c03
705 HTTP_CURL_INFO(NUM_CONNECTS);
706 #endif
707 }
708 /* }}} */
709
710 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
711 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
712 {
713 STATUS status;
714 zend_bool clean_curl;
715
716 if ((clean_curl = (!ch))) {
717 if (!(ch = curl_easy_init())) {
718 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl.");
719 return FAILURE;
720 }
721 }
722
723 status = ((SUCCESS == http_request_init(ch, meth, url, body, options, response)) &&
724 (SUCCESS == http_request_exec(ch, info))) ? SUCCESS : FAILURE;
725
726 if (clean_curl) {
727 curl_easy_cleanup(ch);
728 }
729 return status;
730 }
731 /* }}} */
732
733 /* {{{ char *http_request_method_name(http_request_method) */
734 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
735 {
736 zval **meth;
737
738 if (HTTP_STD_REQUEST_METHOD(m)) {
739 return http_request_methods[m];
740 }
741
742 if (SUCCESS == zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(m), (void **) &meth)) {
743 return Z_STRVAL_PP(meth);
744 }
745
746 return http_request_methods[0];
747 }
748 /* }}} */
749
750 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
751 PHP_HTTP_API unsigned long _http_request_method_exists(zend_bool by_name, unsigned long id, const char *name TSRMLS_DC)
752 {
753 if (by_name) {
754 unsigned i;
755
756 for (i = HTTP_NO_REQUEST_METHOD + 1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
757 if (!strcmp(name, http_request_methods[i])) {
758 return i;
759 }
760 }
761 {
762 zval **data;
763 char *key;
764 ulong idx;
765
766 FOREACH_HASH_KEYVAL(&HTTP_G(request).methods.custom, key, idx, data) {
767 if (!strcmp(name, Z_STRVAL_PP(data))) {
768 return idx + HTTP_MAX_REQUEST_METHOD;
769 }
770 }
771 }
772 return 0;
773 } else {
774 return HTTP_STD_REQUEST_METHOD(id) || zend_hash_index_exists(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(id)) ? id : 0;
775 }
776 }
777 /* }}} */
778
779 /* {{{ unsigned long http_request_method_register(char *) */
780 PHP_HTTP_API unsigned long _http_request_method_register(const char *method TSRMLS_DC)
781 {
782 zval array;
783 char *http_method;
784 unsigned long meth_num = HTTP_G(request).methods.custom.nNextFreeElement + HTTP_MAX_REQUEST_METHOD;
785
786 Z_ARRVAL(array) = &HTTP_G(request).methods.custom;
787 add_next_index_string(&array, estrdup(method), 0);
788
789 spprintf(&http_method, 0, "HTTP_%s", method);
790 zend_register_long_constant(http_method, strlen(http_method) + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
791 efree(http_method);
792
793 return meth_num;
794 }
795 /* }}} */
796
797 /* {{{ STATUS http_request_method_unregister(usngigned long) */
798 PHP_HTTP_API STATUS _http_request_method_unregister(unsigned long method TSRMLS_DC)
799 {
800 zval **zmethod;
801 char *http_method;
802
803 if (SUCCESS != zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method), (void **) &zmethod)) {
804 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Request method with id %lu does not exist", method);
805 return FAILURE;
806 }
807
808 spprintf(&http_method, 0, "HTTP_%s", Z_STRVAL_PP(zmethod));
809
810 if ( (SUCCESS != zend_hash_index_del(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method)))
811 || (SUCCESS != zend_hash_del(EG(zend_constants), http_method, strlen(http_method) + 1))) {
812 http_error_ex(E_NOTICE, 0, "Could not unregister request method: %s", http_method);
813 efree(http_method);
814 return FAILURE;
815 }
816
817 efree(http_method);
818 return SUCCESS;
819 }
820 /* }}} */
821
822 #ifdef ZEND_ENGINE_2
823 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
824 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
825 {
826 zend_bool free_pool;
827 #if HTTP_DEBUG_REQPOOLS
828 fprintf(stderr, "Initializing request pool\n");
829 #endif
830 if ((free_pool = (!pool))) {
831 pool = emalloc(sizeof(http_request_pool));
832 pool->ch = NULL;
833 }
834
835 if (!pool->ch) {
836 if (!(pool->ch = curl_multi_init())) {
837 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl");
838 if (free_pool) {
839 efree(pool);
840 }
841 return NULL;
842 }
843 }
844
845 pool->unfinished = 0;
846 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
847 zend_llist_init(&pool->bodies, sizeof(http_request_body *), (llist_dtor_func_t) http_request_pool_freebody, 0);
848 #if HTTP_DEBUG_REQPOOLS
849 fprintf(stderr, "Initialized request pool %p\n", pool);
850 #endif
851 return pool;
852 }
853 /* }}} */
854
855 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
856 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
857 {
858 getObjectEx(http_request_object, req, request);
859 #if HTTP_DEBUG_REQPOOLS
860 fprintf(stderr, "Attaching request %p to pool %p\n", req, pool);
861 #endif
862 if (req->pool) {
863 http_error(E_WARNING, HTTP_E_CURL, "HttpRequest object is already member of an HttpRequestPool");
864 } else {
865 CURLMcode code;
866 http_request_body *body = http_request_body_new();
867 zval *info = GET_PROP_EX(req, request, responseInfo);
868
869 if (SUCCESS != http_request_object_requesthandler(req, request, body)) {
870 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
871 } else {
872 code = curl_multi_add_handle(pool->ch, req->ch);
873 if ((CURLM_OK != code) && (CURLM_CALL_MULTI_PERFORM != code)) {
874 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
875 } else {
876 req->pool = pool;
877 zval_add_ref(&request);
878 zend_llist_add_element(&pool->handles, &request);
879 zend_llist_add_element(&pool->bodies, &body);
880 return SUCCESS;
881 }
882 }
883 efree(body);
884 }
885 return FAILURE;
886 }
887 /* }}} */
888
889 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
890 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
891 {
892 getObjectEx(http_request_object, req, request);
893 #if HTTP_DEBUG_REQPOOLS
894 fprintf(stderr, "Detaching request %p (pool: %p) from pool %p\n", req, req->pool, pool);
895 #endif
896 if (req->pool != pool) {
897 http_error(E_WARNING, HTTP_E_CURL, "HttpRequest object is not attached to this HttpRequestPool");
898 } else {
899 CURLMcode code;
900
901 if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
902 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
903 } else {
904 req->pool = NULL;
905 zval_ptr_dtor(&request);
906 return SUCCESS;
907 }
908 }
909 return FAILURE;
910 }
911 /* }}} */
912
913 /* {{{ void http_request_pool_detach_all(http_request_pool *) */
914 PHP_HTTP_API void _http_request_pool_detach_all(http_request_pool *pool TSRMLS_DC)
915 {
916 #if HTTP_DEBUG_REQPOOLS
917 fprintf(stderr, "Detaching all requests from pool %p\n", pool);
918 #endif
919 zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_freehandle, pool TSRMLS_CC);
920 }
921
922
923 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
924 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
925 {
926 #if HTTP_DEBUG_REQPOOLS
927 fprintf(stderr, "Attempt to send requests of pool %p\n", pool);
928 #endif
929 while (http_request_pool_perform(pool)) {
930 #if HTTP_DEBUG_REQPOOLS
931 fprintf(stderr, "%d unfinished requests of pool %p remaining\n", pool->unfinished, pool);
932 #endif
933 if (SUCCESS != http_request_pool_select(pool)) {
934 http_error(E_WARNING, HTTP_E_CURL, "Socket error");
935 return FAILURE;
936 }
937 }
938 zend_llist_apply(&pool->handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
939 return SUCCESS;
940 }
941 /* }}} */
942
943 /* {{{ void http_request_pool_dtor(http_request_pool *) */
944 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
945 {
946 #if HTTP_DEBUG_REQPOOLS
947 fprintf(stderr, "Destructing request pool %p\n", pool);
948 #endif
949 pool->unfinished = 0;
950 zend_llist_clean(&pool->handles);
951 zend_llist_clean(&pool->bodies);
952 curl_multi_cleanup(pool->ch);
953 }
954 /* }}} */
955
956 /* {{{ STATUS http_request_pool_select(http_request_pool *) */
957 PHP_HTTP_API STATUS _http_request_pool_select(http_request_pool *pool)
958 {
959 int MAX;
960 fd_set R, W, E;
961 struct timeval timeout = {1, 0};
962
963 FD_ZERO(&R);
964 FD_ZERO(&W);
965 FD_ZERO(&E);
966
967 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
968 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
969 }
970 /* }}} */
971
972 /* {{{ int http_request_pool_perform(http_request_pool *) */
973 PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool)
974 {
975 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
976 return pool->unfinished;
977 }
978 /* }}} */
979
980 /* {{{ void http_request_pool_responsehandler(zval **) */
981 void _http_request_pool_responsehandler(zval **req TSRMLS_DC)
982 {
983 getObjectEx(http_request_object, obj, *req);
984 #if HTTP_DEBUG_REQPOOLS
985 fprintf(stderr, "Fetching data from request %p of pool %p\n", obj, obj->pool);
986 #endif
987 http_request_object_responsehandler(obj, *req);
988 }
989 /* }}} */
990
991 /*#*/
992
993 /* {{{ static void http_request_pool_freebody(http_request_body **) */
994 static void http_request_pool_freebody(http_request_body **body)
995 {
996 TSRMLS_FETCH();
997 http_request_body_free(*body);
998 }
999 /* }}} */
1000
1001 /* {{{ static void http_request_pool_freehandle(zval **, http_request_pool *) */
1002 static void http_request_pool_freehandle(zval **request, http_request_pool *pool TSRMLS_DC)
1003 {
1004 getObjectEx(http_request_object, req, *request);
1005 if (req->pool) {
1006 http_request_pool_detach(pool, *request);
1007 }
1008 #if HTTP_DEBUG_REQPOOLS
1009 else fprintf(stderr, "Request %p (pool: %p) is not (anymore) attached to pool %p\n", req, req->pool, pool);
1010 #endif
1011 }
1012 /* }}} */
1013 #endif /* ZEND_ENGINE_2 */
1014
1015 /* {{{ char *http_request_methods[] */
1016 static const char *const http_request_methods[] = {
1017 "UNKOWN",
1018 /* HTTP/1.1 */
1019 "GET",
1020 "HEAD",
1021 "POST",
1022 "PUT",
1023 "DELETE",
1024 "OPTIONS",
1025 "TRACE",
1026 "CONNECT",
1027 /* WebDAV - RFC 2518 */
1028 "PROPFIND",
1029 "PROPPATCH",
1030 "MKCOL",
1031 "COPY",
1032 "MOVE",
1033 "LOCK",
1034 "UNLOCK",
1035 /* WebDAV Versioning - RFC 3253 */
1036 "VERSION-CONTROL",
1037 "REPORT",
1038 "CHECKOUT",
1039 "CHECKIN",
1040 "UNCHECKOUT",
1041 "MKWORKSPACE",
1042 "UPDATE",
1043 "LABEL",
1044 "MERGE",
1045 "BASELINE-CONTROL",
1046 "MKACTIVITY",
1047 /* WebDAV Access Control - RFC 3744 */
1048 "ACL",
1049 NULL
1050 };
1051 /* }}} */
1052
1053 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
1054 static size_t http_curl_write_callback(char *buf, size_t len, size_t n, void *s)
1055 {
1056 HTTP_CURL_CALLBACK_DATA(s, phpstr *, str);
1057 return str ? phpstr_append(PHPSTR(str), buf, len * n) : len * n;
1058 }
1059 /* }}} */
1060
1061 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
1062 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
1063 {
1064 static char *offset = NULL, *original = NULL;
1065 HTTP_CURL_CALLBACK_DATA(s, http_request_body *, body);
1066
1067 if (body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
1068 return 0;
1069 }
1070 return php_stream_read((php_stream *) body->data, data, len * n);
1071 }
1072 /* }}} */
1073
1074 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
1075 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC)
1076 {
1077 http_curl_callback_ctx *ctx = emalloc(sizeof(http_curl_callback_ctx));
1078 TSRMLS_SET_CTX(ctx->tsrm_ctx);
1079 ctx->data = data;
1080 return http_request_data_copy(COPY_CONTEXT, ctx);
1081 }
1082 /* }}} */
1083
1084 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1085 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
1086 {
1087 zval *params_pass[4], params_local[4], retval;
1088 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
1089
1090 params_pass[0] = &params_local[0];
1091 params_pass[1] = &params_local[1];
1092 params_pass[2] = &params_local[2];
1093 params_pass[3] = &params_local[3];
1094
1095 ZVAL_DOUBLE(params_pass[0], dltotal);
1096 ZVAL_DOUBLE(params_pass[1], dlnow);
1097 ZVAL_DOUBLE(params_pass[2], ultotal);
1098 ZVAL_DOUBLE(params_pass[3], ulnow);
1099
1100 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
1101 }
1102 /* }}} */
1103
1104 static int http_curl_debug_callback(CURL *ch, curl_infotype type, char *string, size_t length, void *data)
1105 {
1106 zval *params_pass[2], params_local[2], retval;
1107 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
1108
1109 params_pass[0] = &params_local[0];
1110 params_pass[1] = &params_local[1];
1111
1112 ZVAL_LONG(params_pass[0], type);
1113 ZVAL_STRINGL(params_pass[1], string, length, 1);
1114
1115 call_user_function(EG(function_table), NULL, func, &retval, 2, params_pass TSRMLS_CC);
1116
1117 return 0;
1118 }
1119 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
1120 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
1121 {
1122 zval **zoption;
1123
1124 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
1125 return NULL;
1126 }
1127
1128 if (Z_TYPE_PP(zoption) != type) {
1129 switch (type)
1130 {
1131 case IS_BOOL: convert_to_boolean_ex(zoption); break;
1132 case IS_LONG: convert_to_long_ex(zoption); break;
1133 case IS_DOUBLE: convert_to_double_ex(zoption); break;
1134 case IS_STRING: convert_to_string_ex(zoption); break;
1135 case IS_ARRAY: convert_to_array_ex(zoption); break;
1136 case IS_OBJECT: convert_to_object_ex(zoption); break;
1137 default:
1138 break;
1139 }
1140 }
1141
1142 return *zoption;
1143 }
1144 /* }}} */
1145
1146 /*
1147 * Local variables:
1148 * tab-width: 4
1149 * c-basic-offset: 4
1150 * End:
1151 * vim600: noet sw=4 ts=4 fdm=marker
1152 * vim<600: noet sw=4 ts=4
1153 */
1154