Skip to content

Commit

Permalink
co http2 server (#2894)
Browse files Browse the repository at this point in the history
* co http2 server

* add conn info

* fix bugs

* add test, fix bug

* Use socket method
  • Loading branch information
matyhtf committed Oct 19, 2019
1 parent 8f0f87e commit 5479dbc
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 144 deletions.
9 changes: 7 additions & 2 deletions examples/coroutine/http/server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php
Co::set([
'trace_flags' => SWOOLE_TRACE_HTTP2,
'log_level' => 0,
]);
go(function () {
$server = new Co\Http\Server("127.0.0.1", 9501, $ssl);
$server = new Co\Http\Server("127.0.0.1", 9501, false);
/**
* 静态文件处理器
*/
Expand Down Expand Up @@ -33,6 +37,7 @@
* Http应用
*/
$server->handle('/', function ($request, $response) {
var_dump($request);
var_dump($request->get);
$response->end("<h1>hello world</h1>");
});
Expand All @@ -45,4 +50,4 @@
$server->start();
});

swoole_event_wait();
swoole_event_wait();
4 changes: 4 additions & 0 deletions examples/http2/server.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
Co::set([
'trace_flags' => SWOOLE_TRACE_HTTP2,
'log_level' => 0,
]);
$key_dir = dirname(__DIR__) . '/ssl';
$http = new swoole_http_server("0.0.0.0", 9501, SWOOLE_BASE);
//$http = new swoole_http_server("0.0.0.0", 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL);
Expand Down
6 changes: 0 additions & 6 deletions src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1556,12 +1556,6 @@ ssize_t Socket::recv_packet(double timeout)
header_len = protocol.real_header_length;
goto _recv_header;
}
//empty package
else if (buf_len == header_len)
{
read_buffer->length = 0;
return header_len;
}
else if (buf_len > protocol.package_max_length)
{
set_err(SW_ERROR_PACKAGE_LENGTH_TOO_LARGE, "remote packet is too big");
Expand Down
2 changes: 1 addition & 1 deletion src/server/process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static bool inline process_is_supported_send_yield(swServer *serv, swConnection
}
else
{
return swServer_worker_schedule(serv, conn->fd, nullptr) == SwooleWG.id;
return swServer_worker_schedule(serv, conn->fd, nullptr) == (int) SwooleWG.id;
}
}

Expand Down
46 changes: 45 additions & 1 deletion swoole_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "thirdparty/swoole_http_parser.h"
#include "thirdparty/multipart_parser.h"

#include <unordered_map>

#ifdef SW_HAVE_ZLIB
#include <zlib.h>
#define SW_ZLIB_ENCODING_RAW -0xf
Expand Down Expand Up @@ -116,6 +118,7 @@ struct http_context
#endif
uint32_t chunk :1;
uint32_t keepalive :1;
uint32_t http2 :1;
uint32_t websocket :1;
uint32_t upgrade :1;
uint32_t detached :1;
Expand Down Expand Up @@ -156,6 +159,45 @@ struct http_context
bool (*close)(http_context* ctx);
};

class http2_stream
{
public:
http_context* ctx;
// uint8_t priority; // useless now
uint32_t id;
// flow control
uint32_t send_window;
uint32_t recv_window;

http2_stream(int _fd, uint32_t _id);
~http2_stream();
void reset(uint32_t error_code);
};

class http2_session
{
public:
int fd;
std::unordered_map<int, http2_stream*> streams;

nghttp2_hd_inflater *inflater = nullptr;
nghttp2_hd_deflater *deflater = nullptr;

uint32_t header_table_size;
uint32_t send_window;
uint32_t recv_window;
uint32_t max_concurrent_streams;
uint32_t max_frame_size;

http_context *default_ctx = nullptr;
void *private_data = nullptr;

void (*handle)(http2_session *, http2_stream *) = nullptr;

http2_session(int _fd);
~http2_session();
};

extern zend_class_entry *swoole_http_server_ce;
extern zend_class_entry *swoole_http_request_ce;
extern zend_class_entry *swoole_http_response_ce;
Expand All @@ -173,6 +215,8 @@ extern swString *swoole_zlib_buffer;
http_context* swoole_http_context_new(int fd);
http_context* swoole_http_context_get(zval *zobject, const bool check_end);
void swoole_http_context_free(http_context *ctx);
void swoole_http_context_copy(http_context *src, http_context *dst);

static sw_inline zval* swoole_http_init_and_read_property(zend_class_entry *ce, zval *zobject, zval** zproperty_store_pp, const char *name, size_t name_len)
{
if (UNEXPECTED(!*zproperty_store_pp))
Expand All @@ -187,7 +231,6 @@ static sw_inline zval* swoole_http_init_and_read_property(zend_class_entry *ce,
}
int swoole_http_parse_form_data(http_context *ctx, const char *boundary_str, int boundary_len);
void swoole_http_parse_cookie(zval *array, const char *at, size_t length);

void swoole_http_server_init_context(swServer *serv, http_context *ctx);

size_t swoole_http_requset_parse(http_context *ctx, const char *data, size_t length);
Expand Down Expand Up @@ -261,6 +304,7 @@ bool swoole_websocket_handshake(http_context *ctx);

#ifdef SW_USE_HTTP2
int swoole_http2_server_onFrame(swServer *serv, swConnection *conn, swEventData *req);
int swoole_http2_server_parse(http2_session *client, char *buf);
int swoole_http2_server_do_response(http_context *ctx, swString *body);
void swoole_http2_server_session_free(swConnection *conn);
void swoole_http2_response_end(http_context *ctx, zval *zdata, zval *return_value);
Expand Down
Loading

0 comments on commit 5479dbc

Please sign in to comment.