if (isset($ifdefs[$short])) printf("#endif\n");
}
-file_put_contents("php_http_curl.c",
+file_put_contents("php_http_curl_client.c",
preg_replace('/(\/\* BEGIN::CURLINFO \*\/\n).*(\n\s*\/\* END::CURLINFO \*\/)/s', '$1'. ob_get_contents() .'$2',
- file_get_contents("php_http_curl.c")));
+ file_get_contents("php_http_curl_client.c")));
?>
with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a!z!a!/", &meth_str, &meth_len, &url_str, &url_len, &zheader, &zbody, &zoptions)) {
- php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
php_http_message_object_t *msg_obj;
zend_object_value ov;
zval *req, *res = NULL;
if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
}
+
+#if PHP_HTTP_CURL_VERSION(7,25,0)
+ /* tcp */
+ if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepalive"), IS_BOOL))) {
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPALIVE, (long)Z_BVAL_P(zoption));
+ }
+ if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepidle"), IS_LONG))) {
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPIDLE, Z_LVAL_P(zoption));
+ }
+ if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepintvl"), IS_LONG))) {
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPINTVL, Z_LVAL_P(zoption));
+ }
+#endif
/* ssl */
if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) {
curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L);
curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L);
curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L);
- curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3);
+ curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3L);
+#if PHP_HTTP_CURL_VERSION(7,25,0)
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPALIVE, 0L);
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPIDLE, 60L);
+ curl_easy_setopt(ch, CURLOPT_TCP_KEEPINTVL, 60L);
+#endif
curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL);
curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL);
curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL);
return message;
}
-PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len TSRMLS_DC)
+PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC)
{
php_http_message_parser_t p;
php_http_buffer_t buf;
+ unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
int free_msg;
php_http_buffer_from_string_ex(&buf, str, len);
msg = php_http_message_init(NULL, 0 TSRMLS_CC);
}
- if (FAILURE == php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg)) {
+ if (greedy) {
+ flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
+ }
+ if (FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
if (free_msg) {
php_http_message_free(&msg);
}
PHP_HTTP_BEGIN_ARGS(__construct, 0)
PHP_HTTP_ARG_VAL(message, 0)
+ PHP_HTTP_ARG_VAL(greedy, 0)
PHP_HTTP_END_ARGS;
PHP_HTTP_EMPTY_ARGS(getBody);
if (i > 1) {
zend_object_value *ovalues = NULL;
php_http_message_object_t **objects = NULL;
- int last = i - 1;
+ int last;
objects = ecalloc(i, sizeof(**objects));
ovalues = ecalloc(i, sizeof(*ovalues));
PHP_METHOD(HttpMessage, __construct)
{
+ zend_bool greedy = 1;
zval *zmessage = NULL;
php_http_message_t *msg = NULL;
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
- if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) {
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!b", &zmessage, &greedy) && zmessage) {
if (Z_TYPE_P(zmessage) == IS_RESOURCE) {
php_stream *s;
php_http_message_parser_t p;
php_stream_from_zval(s, &zmessage);
if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
- php_http_message_parser_parse_stream(&p, s, &msg);
+ unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
+
+ php_http_message_parser_parse_stream(&p, s, flags, &msg);
php_http_message_parser_dtor(&p);
}
+
+ if (!msg) {
+ php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message from stream");
+ }
} else {
zmessage = php_http_ztyp(IS_STRING, zmessage);
- msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC);
+ if (!(msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage), greedy TSRMLS_CC))) {
+ php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", MIN(25, Z_STRLEN_P(zmessage)), Z_STRVAL_P(zmessage));
+ }
zval_ptr_dtor(&zmessage);
}
if (obj->message->parent) {
obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
}
- } else {
- php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, Z_STRVAL_P(zmessage));
}
}
if (!obj->message) {
if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_get_class_entry())) {
php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
- php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
if (!obj->message) {
obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
php_http_message_dtor(obj->message);
efree(obj->message);
}
- if ((msg = php_http_message_parse(NULL, serialized, (size_t) length TSRMLS_CC))) {
+ if ((msg = php_http_message_parse(NULL, serialized, (size_t) length, 1 TSRMLS_CC))) {
obj->message = msg;
} else {
obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
for (c = 0; __tmp_msg; __tmp_msg = __tmp_msg->parent, ++(c)); \
}
-PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len TSRMLS_DC);
+PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC);
/* PHP */
#include "php_http_api.h"
+#ifndef DBG_PARSER
+# define DBG_PARSER 0
+#endif
+
typedef struct php_http_message_parser_state_spec {
php_http_message_parser_state_t state;
unsigned need_data:1;
{PHP_HTTP_MESSAGE_PARSER_STATE_DONE, 0}
};
+#if DBG_PARSER
+const char *php_http_message_parser_state_name(php_http_message_parser_state_t state) {
+ const char *states[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "DONE"};
+
+ if (state < 0 || state > (sizeof(states)/sizeof(char*))-1) {
+ return "FAILURE";
+ }
+ return states[state];
+}
+#endif
+
PHP_HTTP_API php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser TSRMLS_DC)
{
if (!parser) {
}
}
-PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, php_http_message_t **message)
+PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, unsigned flags, php_http_message_t **message)
{
php_http_buffer_t buf;
+ php_http_message_parser_state_t state = PHP_HTTP_MESSAGE_PARSER_STATE_START;
TSRMLS_FETCH_FROM_CTX(parser->ts);
php_http_buffer_init_ex(&buf, 0x1000, PHP_HTTP_BUFFER_INIT_PREALLOC);
while (!php_stream_eof(s)) {
size_t len = 0;
-
- switch (php_http_message_parser_state_is(parser)) {
+#if DBG_PARSER
+ fprintf(stderr, "#SP: %s (f:%u)\n", php_http_message_parser_state_name(state), flags);
+#endif
+ switch (state) {
case PHP_HTTP_MESSAGE_PARSER_STATE_START:
case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER:
case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
return php_http_message_parser_state_is(parser);
}
- php_http_message_parser_parse(parser, &buf, 0, message);
+ state = php_http_message_parser_parse(parser, &buf, flags, message);
}
php_http_buffer_dtor(&buf);
TSRMLS_FETCH_FROM_CTX(parser->ts);
while (buffer->used || !php_http_message_parser_states[php_http_message_parser_state_is(parser)].need_data) {
-#if 0
- const char *state[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "DONE"};
- fprintf(stderr, "#MP: %s (%d)\n", php_http_message_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_message_parser_state_is(parser)], message && *message ? (*message)->type : -1);
+#if DBG_PARSER
+ fprintf(stderr, "#MP: %s (f: %u, t:%d, l:%zu)\n",
+ php_http_message_parser_state_name(php_http_message_parser_state_is(parser)),
+ flags,
+ message && *message ? (*message)->type : -1,
+ buffer->used
+ );
_dpf(0, buffer->data, buffer->used);
#endif
}
php_http_buffer_cut(buffer, 0, ptr - buffer->data);
+
+ if (!(flags & PHP_HTTP_MESSAGE_PARSER_GREEDY)) {
+ return PHP_HTTP_MESSAGE_PARSER_STATE_DONE;
+ }
break;
}
}
#define PHP_HTTP_MESSAGE_PARSER_CLEANUP 0x1
#define PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES 0x2
#define PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS 0x4
+#define PHP_HTTP_MESSAGE_PARSER_GREEDY 0x8
typedef struct php_http_message_parser {
php_http_header_parser_t header;
PHP_HTTP_API void php_http_message_parser_dtor(php_http_message_parser_t *parser);
PHP_HTTP_API void php_http_message_parser_free(php_http_message_parser_t **parser);
PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse(php_http_message_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, php_http_message_t **message);
-PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, php_http_message_t **message);
+PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_stream *s, unsigned flags, php_http_message_t **message);
#endif