Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# https://editorconfig.org/

root = true

[*]
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
charset = utf-8
tab_width = 4

[{*.{c,h,m4},Makefile*}]
indent_size = 4
indent_style = tab

[*.{md,php,phpt,stub.php,yml}]
indent_size = 4
indent_style = space
3 changes: 2 additions & 1 deletion drivers/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ static void websocket_epoll_unwatch(const int fd)
static int websocket_epoll_wait(const int timeout_usec, int *ready_fd)
{
struct epoll_event event;
int ready;

if (epoll_fd < 0) {
errno = EBADF;
return -1;
}

const int ready = epoll_wait(epoll_fd, &event, 1, timeout_usec / 1000);
ready = epoll_wait(epoll_fd, &event, 1, timeout_usec / 1000);
if (ready > 0) {
*ready_fd = event.data.fd;
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static int websocket_kqueue_wait(const int timeout_usec, int *ready_fd)
{
struct kevent event;
struct timespec timeout;
int ready;

if (kqueue_fd < 0) {
errno = EBADF;
Expand All @@ -124,7 +125,7 @@ static int websocket_kqueue_wait(const int timeout_usec, int *ready_fd)
timeout.tv_sec = timeout_usec / 1000000;
timeout.tv_nsec = (timeout_usec % 1000000) * 1000;

const int ready = kevent(kqueue_fd, NULL, 0, &event, 1, &timeout);
ready = kevent(kqueue_fd, NULL, 0, &event, 1, &timeout);
if (ready < 0) {
return -1;
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ static void websocket_poll_unwatch(const int fd)
static int websocket_poll_wait(const int timeout_usec, int *ready_fd)
{
size_t i;
int ready;

if (poll_fd_count == 0) {
errno = EBADF;
return -1;
}

const int ready = poll(poll_fds, (nfds_t) poll_fd_count, timeout_usec / 1000);
ready = poll(poll_fds, (nfds_t) poll_fd_count, timeout_usec / 1000);
if (ready <= 0) {
return ready;
}
Expand Down
3 changes: 2 additions & 1 deletion drivers/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static int websocket_select_wait(const int timeout_usec, int *ready_fd)
struct timeval timeout;
int max_fd = -1;
size_t i;
int ready;

if (select_fd_count == 0) {
errno = EBADF;
Expand All @@ -175,7 +176,7 @@ static int websocket_select_wait(const int timeout_usec, int *ready_fd)
timeout.tv_sec = timeout_usec / 1000000;
timeout.tv_usec = timeout_usec % 1000000;

const int ready = select(max_fd + 1, &read_fds, &write_fds, NULL, &timeout);
ready = select(max_fd + 1, &read_fds, &write_fds, NULL, &timeout);
if (ready <= 0) {
return ready;
}
Expand Down
10 changes: 7 additions & 3 deletions websocket_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ PHP_METHOD(WebSocket_Connection, send)
zend_string *payload;
zval *type = NULL;
websocket_connection_object *intern = Z_WEBSOCKET_CONNECTION_P(ZEND_THIS);
uint8_t opcode;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_STR(payload)
Expand All @@ -469,7 +470,7 @@ PHP_METHOD(WebSocket_Connection, send)
RETURN_THROWS();
}

const uint8_t opcode = type ? websocket_protocol_message_type_opcode(type) : WEBSOCKET_OPCODE_TEXT;
opcode = type ? websocket_protocol_message_type_opcode(type) : WEBSOCKET_OPCODE_TEXT;
if (opcode == WEBSOCKET_OPCODE_CONTINUATION) {
zend_argument_value_error(2, "must not be WebSocket\\MessageType::Continuation");
RETURN_THROWS();
Expand All @@ -489,6 +490,7 @@ PHP_METHOD(WebSocket_Connection, close)
{
zend_long code = 1000;
zend_string *reason = NULL;
websocket_connection_object *intern;

if (ZEND_NUM_ARGS() > 0) {
ZEND_PARSE_PARAMETERS_START(0, 2)
Expand All @@ -511,7 +513,7 @@ PHP_METHOD(WebSocket_Connection, close)
RETURN_THROWS();
}

websocket_connection_object *intern = Z_WEBSOCKET_CONNECTION_P(ZEND_THIS);
intern = Z_WEBSOCKET_CONNECTION_P(ZEND_THIS);
if (intern->defer_close) {
intern->open = false;
return;
Expand All @@ -526,9 +528,11 @@ PHP_METHOD(WebSocket_Connection, close)

PHP_METHOD(WebSocket_Connection, isOpen)
{
websocket_connection_object *intern;

ZEND_PARSE_PARAMETERS_NONE();

websocket_connection_object *intern = Z_WEBSOCKET_CONNECTION_P(ZEND_THIS);
intern = Z_WEBSOCKET_CONNECTION_P(ZEND_THIS);

RETURN_BOOL(intern->open && !intern->close_after_write);
}
Expand Down
18 changes: 9 additions & 9 deletions websocket_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static bool websocket_http_validate_request_line(const char *line, const size_t
}

method_len = (size_t) (method_end - line);
if (!websocket_http_equals_ci(line, method_len, "GET", sizeof("GET") - 1)) {
if (!websocket_http_equals_ci(line, method_len, "GET", strlen("GET"))) {
return false;
}

Expand All @@ -115,7 +115,7 @@ static bool websocket_http_validate_request_line(const char *line, const size_t
version_start = target_end + 1;
version_len = (size_t) ((line + line_len) - version_start);

return websocket_http_equals_ci(version_start, version_len, "HTTP/1.1", sizeof("HTTP/1.1") - 1);
return websocket_http_equals_ci(version_start, version_len, "HTTP/1.1", strlen("HTTP/1.1"));
}

static bool websocket_http_validate_key(const char *value, const size_t value_len, zend_string **accept_key)
Expand Down Expand Up @@ -212,13 +212,13 @@ websocket_http_upgrade_result websocket_http_parse_upgrade(const char *buffer, c
websocket_http_trim(&name, &name_len);
websocket_http_trim(&value, &value_len);

if (websocket_http_equals_ci(name, name_len, "Upgrade", sizeof("Upgrade") - 1)) {
has_upgrade = websocket_http_header_contains_token(value, value_len, "websocket", sizeof("websocket") - 1);
} else if (websocket_http_equals_ci(name, name_len, "Connection", sizeof("Connection") - 1)) {
has_connection = websocket_http_header_contains_token(value, value_len, "upgrade", sizeof("upgrade") - 1);
} else if (websocket_http_equals_ci(name, name_len, "Sec-WebSocket-Version", sizeof("Sec-WebSocket-Version") - 1)) {
has_version = websocket_http_equals_ci(value, value_len, "13", sizeof("13") - 1);
} else if (websocket_http_equals_ci(name, name_len, "Sec-WebSocket-Key", sizeof("Sec-WebSocket-Key") - 1)) {
if (websocket_http_equals_ci(name, name_len, "Upgrade", strlen("Upgrade"))) {
has_upgrade = websocket_http_header_contains_token(value, value_len, "websocket", strlen("websocket"));
} else if (websocket_http_equals_ci(name, name_len, "Connection", strlen("Connection"))) {
has_connection = websocket_http_header_contains_token(value, value_len, "upgrade", strlen("upgrade"));
} else if (websocket_http_equals_ci(name, name_len, "Sec-WebSocket-Version", strlen("Sec-WebSocket-Version"))) {
has_version = websocket_http_equals_ci(value, value_len, "13", strlen("13"));
} else if (websocket_http_equals_ci(name, name_len, "Sec-WebSocket-Key", strlen("Sec-WebSocket-Key"))) {
if (*accept_key) {
zend_string_release(*accept_key);
*accept_key = NULL;
Expand Down
69 changes: 38 additions & 31 deletions websocket_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "ext/standard/sha1.h"
#include "Zend/zend_enum.h"

#include <string.h>

#define WEBSOCKET_GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

static uint32_t websocket_frame_prop_type_num;
Expand All @@ -30,7 +32,7 @@ zend_string *websocket_protocol_accept_key(zend_string *key)

PHP_SHA1Init(&ctx);
PHP_SHA1Update(&ctx, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
PHP_SHA1Update(&ctx, (unsigned char *) WEBSOCKET_GUID, sizeof(WEBSOCKET_GUID) - 1);
PHP_SHA1Update(&ctx, (unsigned char *) WEBSOCKET_GUID, strlen(WEBSOCKET_GUID));
PHP_SHA1Final(digest, &ctx);

return php_base64_encode(digest, sizeof(digest));
Expand Down Expand Up @@ -223,12 +225,12 @@ zend_string *websocket_protocol_pack_payload(zend_string *payload, uint8_t opcod

static void websocket_frame_update_properties(zval *object, zval *type, zend_string *payload, bool final, zend_long bytes_consumed, zend_long opcode, zend_long flags)
{
zend_update_property(websocket_frame_ce, Z_OBJ_P(object), "type", sizeof("type") - 1, type);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "opcode", sizeof("opcode") - 1, opcode);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "flags", sizeof("flags") - 1, flags);
zend_update_property_str(websocket_frame_ce, Z_OBJ_P(object), "payload", sizeof("payload") - 1, payload);
zend_update_property_bool(websocket_frame_ce, Z_OBJ_P(object), "final", sizeof("final") - 1, final);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "bytesConsumed", sizeof("bytesConsumed") - 1, bytes_consumed);
zend_update_property(websocket_frame_ce, Z_OBJ_P(object), "type", strlen("type"), type);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "opcode", strlen("opcode"), opcode);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "flags", strlen("flags"), flags);
zend_update_property_str(websocket_frame_ce, Z_OBJ_P(object), "payload", strlen("payload"), payload);
zend_update_property_bool(websocket_frame_ce, Z_OBJ_P(object), "final", strlen("final"), final);
zend_update_property_long(websocket_frame_ce, Z_OBJ_P(object), "bytesConsumed", strlen("bytesConsumed"), bytes_consumed);
}

static zend_always_inline void websocket_frame_init_properties(zval *object, zval *type, zend_string *payload, bool final, zend_long bytes_consumed, zend_long opcode, zend_long flags)
Expand Down Expand Up @@ -256,10 +258,10 @@ static zend_always_inline void websocket_frame_init_properties(zval *object, zva

static void websocket_close_frame_update_properties(zval *object, zend_long code, zend_string *reason, zend_long flags, zend_long bytes_consumed)
{
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "code", sizeof("code") - 1, code);
zend_update_property_str(websocket_close_frame_ce, Z_OBJ_P(object), "reason", sizeof("reason") - 1, reason);
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "flags", sizeof("flags") - 1, flags);
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "bytesConsumed", sizeof("bytesConsumed") - 1, bytes_consumed);
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "code", strlen("code"), code);
zend_update_property_str(websocket_close_frame_ce, Z_OBJ_P(object), "reason", strlen("reason"), reason);
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "flags", strlen("flags"), flags);
zend_update_property_long(websocket_close_frame_ce, Z_OBJ_P(object), "bytesConsumed", strlen("bytesConsumed"), bytes_consumed);
}

static zend_always_inline void websocket_close_frame_init_properties(zval *object, zend_long code, zend_string *reason, zend_long flags, zend_long bytes_consumed)
Expand All @@ -281,12 +283,14 @@ static zend_always_inline void websocket_close_frame_init_properties(zval *objec

zend_string *websocket_protocol_close_payload(zend_long code, zend_string *reason)
{
zend_string *payload;

if (ZSTR_LEN(reason) > WEBSOCKET_CLOSE_REASON_MAX_LEN) {
zend_argument_value_error(2, "must be at most %d bytes", WEBSOCKET_CLOSE_REASON_MAX_LEN);
return NULL;
}

zend_string *payload = zend_string_alloc(2 + ZSTR_LEN(reason), 0);
payload = zend_string_alloc(2 + ZSTR_LEN(reason), 0);
ZSTR_VAL(payload)[0] = (char) ((code >> 8) & 0xff);
ZSTR_VAL(payload)[1] = (char) (code & 0xff);
if (ZSTR_LEN(reason) > 0) {
Expand All @@ -302,6 +306,8 @@ PHP_METHOD(WebSocket_Frame, __construct)
zval *type;
zend_string *payload;
bool final = true;
zend_long opcode;
zend_long flags;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJECT_OF_CLASS(type, websocket_message_type_ce)
Expand All @@ -310,8 +316,8 @@ PHP_METHOD(WebSocket_Frame, __construct)
Z_PARAM_BOOL(final)
ZEND_PARSE_PARAMETERS_END();

const zend_long opcode = websocket_protocol_message_type_opcode(type);
const zend_long flags = final ? WEBSOCKET_FLAG_FIN : 0;
opcode = websocket_protocol_message_type_opcode(type);
flags = final ? WEBSOCKET_FLAG_FIN : 0;

websocket_frame_update_properties(ZEND_THIS, type, payload, final, 0, opcode, flags);
}
Expand Down Expand Up @@ -396,23 +402,23 @@ PHP_METHOD(WebSocket_Protocol, pack)
} else if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), websocket_frame_ce)) {
zval *prop;

prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "payload", sizeof("payload") - 1, 0, &rv);
prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "payload", strlen("payload"), 0, &rv);
payload = zval_get_string(prop);
tmp_payload = payload;

prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "opcode", sizeof("opcode") - 1, 0, &rv);
prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "opcode", strlen("opcode"), 0, &rv);
opcode = zval_get_long(prop);

prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "flags", sizeof("flags") - 1, 0, &rv);
prop = zend_read_property(websocket_frame_ce, Z_OBJ_P(data), "flags", strlen("flags"), 0, &rv);
flags = zval_get_long(prop);
} else if (Z_TYPE_P(data) == IS_OBJECT && instanceof_function(Z_OBJCE_P(data), websocket_close_frame_ce)) {
zval *prop;
zend_long code;

prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "code", sizeof("code") - 1, 0, &rv);
prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "code", strlen("code"), 0, &rv);
code = zval_get_long(prop);

prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "reason", sizeof("reason") - 1, 0, &rv);
prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "reason", strlen("reason"), 0, &rv);
reason = zval_get_string(prop);
payload = websocket_protocol_close_payload(code, reason);
zend_string_release(reason);
Expand All @@ -421,7 +427,7 @@ PHP_METHOD(WebSocket_Protocol, pack)
}
tmp_payload = payload;

prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "flags", sizeof("flags") - 1, 0, &rv);
prop = zend_read_property(websocket_close_frame_ce, Z_OBJ_P(data), "flags", strlen("flags"), 0, &rv);
flags = zval_get_long(prop);
opcode = WEBSOCKET_OPCODE_CLOSE;
} else {
Expand Down Expand Up @@ -566,9 +572,10 @@ static void websocket_protocol_unpack(INTERNAL_FUNCTION_PARAMETERS)
if (opcode == WEBSOCKET_OPCODE_CLOSE) {
zend_long code = WEBSOCKET_CLOSE_NORMAL;
zend_string *reason;
unsigned char *close_payload;

if (payload_len >= 2) {
unsigned char *close_payload = (unsigned char *) ZSTR_VAL(payload);
close_payload = (unsigned char *) ZSTR_VAL(payload);
code = ((zend_long) close_payload[0] << 8) | close_payload[1];
reason = zend_string_init(ZSTR_VAL(payload) + 2, (size_t) payload_len - 2, 0);
} else {
Expand Down Expand Up @@ -613,14 +620,14 @@ void websocket_register_protocol_classes(void)
websocket_close_frame_ce = register_class_WebSocket_CloseFrame();
websocket_protocol_ce = register_class_WebSocket_Protocol();

websocket_frame_prop_type_num = websocket_property_num(websocket_frame_ce, "type", sizeof("type") - 1);
websocket_frame_prop_opcode_num = websocket_property_num(websocket_frame_ce, "opcode", sizeof("opcode") - 1);
websocket_frame_prop_flags_num = websocket_property_num(websocket_frame_ce, "flags", sizeof("flags") - 1);
websocket_frame_prop_payload_num = websocket_property_num(websocket_frame_ce, "payload", sizeof("payload") - 1);
websocket_frame_prop_final_num = websocket_property_num(websocket_frame_ce, "final", sizeof("final") - 1);
websocket_frame_prop_bytes_consumed_num = websocket_property_num(websocket_frame_ce, "bytesConsumed", sizeof("bytesConsumed") - 1);
websocket_close_frame_prop_code_num = websocket_property_num(websocket_close_frame_ce, "code", sizeof("code") - 1);
websocket_close_frame_prop_reason_num = websocket_property_num(websocket_close_frame_ce, "reason", sizeof("reason") - 1);
websocket_close_frame_prop_flags_num = websocket_property_num(websocket_close_frame_ce, "flags", sizeof("flags") - 1);
websocket_close_frame_prop_bytes_consumed_num = websocket_property_num(websocket_close_frame_ce, "bytesConsumed", sizeof("bytesConsumed") - 1);
websocket_frame_prop_type_num = websocket_property_num(websocket_frame_ce, "type", strlen("type"));
websocket_frame_prop_opcode_num = websocket_property_num(websocket_frame_ce, "opcode", strlen("opcode"));
websocket_frame_prop_flags_num = websocket_property_num(websocket_frame_ce, "flags", strlen("flags"));
websocket_frame_prop_payload_num = websocket_property_num(websocket_frame_ce, "payload", strlen("payload"));
websocket_frame_prop_final_num = websocket_property_num(websocket_frame_ce, "final", strlen("final"));
websocket_frame_prop_bytes_consumed_num = websocket_property_num(websocket_frame_ce, "bytesConsumed", strlen("bytesConsumed"));
websocket_close_frame_prop_code_num = websocket_property_num(websocket_close_frame_ce, "code", strlen("code"));
websocket_close_frame_prop_reason_num = websocket_property_num(websocket_close_frame_ce, "reason", strlen("reason"));
websocket_close_frame_prop_flags_num = websocket_property_num(websocket_close_frame_ce, "flags", strlen("flags"));
websocket_close_frame_prop_bytes_consumed_num = websocket_property_num(websocket_close_frame_ce, "bytesConsumed", strlen("bytesConsumed"));
}
4 changes: 2 additions & 2 deletions websocket_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ PHP_METHOD(WebSocket_ServerOptions, __construct)
RETURN_THROWS();
}

zend_update_property_long(websocket_server_options_ce, Z_OBJ_P(ZEND_THIS), "maxMessageSize", sizeof("maxMessageSize") - 1, max_message_size);
zend_update_property_long(websocket_server_options_ce, Z_OBJ_P(ZEND_THIS), "maxQueuedBytes", sizeof("maxQueuedBytes") - 1, max_queued_bytes);
zend_update_property_long(websocket_server_options_ce, Z_OBJ_P(ZEND_THIS), "maxMessageSize", strlen("maxMessageSize"), max_message_size);
zend_update_property_long(websocket_server_options_ce, Z_OBJ_P(ZEND_THIS), "maxQueuedBytes", strlen("maxQueuedBytes"), max_queued_bytes);
}

PHP_METHOD(WebSocket_Server, listen)
Expand Down
Loading
Loading