Skip to content

Commit 163a361

Browse files
committed
Improve code
1 parent 6744494 commit 163a361

5 files changed

+61
-72
lines changed

clear.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
__DIR__=$(cd "$(dirname "$0")";pwd)
33

44
set -e
5-
cd ${__DIR__}
5+
cd "${__DIR__}"
66
set +e
7-
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
8-
find . -name \*.lo -o -name \*.o | xargs rm -f
9-
find . -name \*.la -o -name \*.a | xargs rm -f
10-
find . -name \*.so | xargs rm -f
11-
find . -name .libs -a -type d|xargs rm -rf
12-
rm -f libphp.la modules/* libs/*
7+
find . \( -name \*.gcno -o -name \*.gcda \) -print0 | xargs -0 rm -f
8+
find . \( -name \*.lo -o -name \*.o \) -print0 | xargs -0 rm -f
9+
find . \( -name \*.la -o -name \*.a \) -print0 | xargs -0 rm -f
10+
find . -name \*.so -print0 | xargs -0 rm -f
11+
find . -name .libs -a -type d -print0 | xargs -0 rm -rf
12+
rm -f libphp.la modules/* libs/*

swoole_http.h

-42
Original file line numberDiff line numberDiff line change
@@ -278,48 +278,6 @@ static sw_inline void php_brotli_free(void* opaque, void* address)
278278
}
279279
#endif
280280

281-
static int http_parse_set_cookies(const char *at, size_t length, zval *cookies, zval *zset_cookie_headers)
282-
{
283-
const char *key = at;
284-
zval val;
285-
size_t key_len = 0, val_len = 0;
286-
const char *p, *eof = at + length;
287-
// key
288-
p = (char *) memchr(at, '=', length);
289-
if (p)
290-
{
291-
key_len = p - at;
292-
}
293-
if (key_len == 0 || key_len >= length - 1)
294-
{
295-
swWarn("cookie key format is wrong");
296-
return SW_ERR;
297-
}
298-
if (key_len > SW_HTTP_COOKIE_KEYLEN)
299-
{
300-
swWarn("cookie[%.8s...] name length %zu is exceed the max name len %d", key, key_len, SW_HTTP_COOKIE_KEYLEN);
301-
return SW_ERR;
302-
}
303-
add_next_index_stringl(zset_cookie_headers, (char *) at, length);
304-
// val
305-
p++;
306-
eof = (char*) memchr(p, ';', at + length - p);
307-
if (!eof)
308-
{
309-
eof = at + length;
310-
}
311-
val_len = eof - p;
312-
if (val_len > SW_HTTP_COOKIE_VALLEN)
313-
{
314-
swWarn("cookie[%.*s]'s value[v=%.8s...] length %d is exceed the max value len %d", (int) key_len, key, p, (int) val_len, SW_HTTP_COOKIE_VALLEN);
315-
return SW_ERR;
316-
}
317-
ZVAL_STRINGL(&val, p, val_len);
318-
Z_STRLEN(val) = php_url_decode(Z_STRVAL(val), val_len);
319-
add_assoc_zval_ex(cookies, at, key_len, &val);
320-
return SW_OK;
321-
}
322-
323281
int swoole_websocket_onMessage(swServer *serv, swEventData *req);
324282
int swoole_websocket_onHandshake(swServer *serv, swListenPort *port, http_context *ctx);
325283
void swoole_websocket_onOpen(http_context *ctx);

swoole_http2_client_coro.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ enum swReturn_code http2_client::parse_frame(zval *return_value)
699699
zval zresponse = *stream->zresponse;
700700
if (type == SW_HTTP2_TYPE_RST_STREAM)
701701
{
702-
zend_update_property_long(swoole_http2_response_ce, &zresponse, ZEND_STRL("statusCode"), -3);
702+
zend_update_property_long(swoole_http2_response_ce, &zresponse, ZEND_STRL("statusCode"), -3 /* HTTP_CLIENT_ESTATUS_SERVER_RESET */);
703703
zend_update_property_long(swoole_http2_response_ce, &zresponse, ZEND_STRL("errCode"), value);
704704
}
705705
if (stream->buffer)
@@ -884,6 +884,8 @@ bool http2_client::send_setting()
884884
return send(frame, SW_HTTP2_FRAME_HEADER_SIZE + 18);
885885
}
886886

887+
int http_parse_set_cookies(const char *at, size_t length, zval *cookies, zval *zset_cookie_headers);
888+
887889
int http2_client::parse_header(http2_client_stream *stream, int flags, char *in, size_t inlen)
888890
{
889891
zval *zresponse = stream->zresponse;

swoole_http_client.h

+9-22
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@
1414
+----------------------------------------------------------------------+
1515
*/
1616

17-
#ifndef SWOOLE_HTTP_CLIENT_H_
18-
#define SWOOLE_HTTP_CLIENT_H_
17+
#pragma once
1918

2019
#ifdef __cplusplus
2120
extern "C" {
2221
#endif
2322

24-
#include "ext/standard/basic_functions.h"
25-
#include "ext/standard/php_http.h"
26-
#include "ext/standard/base64.h"
27-
2823
#include "swoole_http.h"
2924
#include "websocket.h"
3025
#include "thirdparty/swoole_http_parser.h"
3126

27+
#include "ext/standard/basic_functions.h"
28+
#include "ext/standard/php_http.h"
29+
#include "ext/standard/base64.h"
30+
3231
#ifdef SW_HAVE_ZLIB
3332
#include <zlib.h>
3433
#endif
3534

35+
#ifdef __cplusplus
36+
}
37+
#endif
38+
3639
enum http_client_error_status_code
3740
{
3841
HTTP_CLIENT_ESTATUS_CONNECT_FAILED = -1,
@@ -41,12 +44,6 @@ enum http_client_error_status_code
4144
HTTP_CLIENT_ESTATUS_SEND_FAILED = -4,
4245
};
4346

44-
enum http_client_error_flags
45-
{
46-
HTTP_CLIENT_EFLAG_TIMEOUT = 1,
47-
HTTP_CLIENT_EFLAG_UPGRADE = 1 << 1,
48-
};
49-
5047
static sw_inline void http_client_create_token(int length, char *buf)
5148
{
5249
char characters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"§$%&/()=[]{}";
@@ -73,13 +70,3 @@ static sw_inline void http_client_append_content_length(swString* buf, int lengt
7370
int n = snprintf(SW_STRS(content_length_str), "Content-Length: %d\r\n\r\n", length);
7471
swString_append_ptr(buf, content_length_str, n);
7572
}
76-
77-
#ifdef SW_HAVE_COMPRESSION
78-
extern swString *swoole_zlib_buffer;
79-
#endif
80-
81-
#ifdef __cplusplus
82-
}
83-
#endif
84-
85-
#endif /* SWOOLE_HTTP_CLIENT_H_ */

swoole_http_client_coro.cc

+42
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,48 @@ static const zend_function_entry swoole_http_client_coro_methods[] =
304304
PHP_FE_END
305305
};
306306

307+
int http_parse_set_cookies(const char *at, size_t length, zval *cookies, zval *zset_cookie_headers)
308+
{
309+
const char *key = at;
310+
zval val;
311+
size_t key_len = 0, val_len = 0;
312+
const char *p, *eof = at + length;
313+
// key
314+
p = (char *) memchr(at, '=', length);
315+
if (p)
316+
{
317+
key_len = p - at;
318+
}
319+
if (key_len == 0 || key_len >= length - 1)
320+
{
321+
swWarn("cookie key format is wrong");
322+
return SW_ERR;
323+
}
324+
if (key_len > SW_HTTP_COOKIE_KEYLEN)
325+
{
326+
swWarn("cookie[%.8s...] name length %zu is exceed the max name len %d", key, key_len, SW_HTTP_COOKIE_KEYLEN);
327+
return SW_ERR;
328+
}
329+
add_next_index_stringl(zset_cookie_headers, (char *) at, length);
330+
// val
331+
p++;
332+
eof = (char*) memchr(p, ';', at + length - p);
333+
if (!eof)
334+
{
335+
eof = at + length;
336+
}
337+
val_len = eof - p;
338+
if (val_len > SW_HTTP_COOKIE_VALLEN)
339+
{
340+
swWarn("cookie[%.*s]'s value[v=%.8s...] length %d is exceed the max value len %d", (int) key_len, key, p, (int) val_len, SW_HTTP_COOKIE_VALLEN);
341+
return SW_ERR;
342+
}
343+
ZVAL_STRINGL(&val, p, val_len);
344+
Z_STRLEN(val) = php_url_decode(Z_STRVAL(val), val_len);
345+
add_assoc_zval_ex(cookies, at, key_len, &val);
346+
return SW_OK;
347+
}
348+
307349
static int http_parser_on_header_field(swoole_http_parser *parser, const char *at, size_t length)
308350
{
309351
http_client* http = (http_client*) parser->data;

0 commit comments

Comments
 (0)