Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[coro_http_server]some improvement and update #582

Merged
merged 1 commit into from
May 20, 2024
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
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,50 @@ async_simple::coro::Lazy<void> test_async_client() {
```

### upload(multipart) file
```cpp
void start_server() {
coro_http_server server(1, 9001);
server.set_http_handler<POST>(
"/form_data",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::multipart);
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
if (part_head.ec) {
co_return;
}

std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n";// if form data, no filename

auto part_body = co_await multipart.read_part_body(boundary);
if (part_body.ec) {
co_return;
}

std::cout << part_body.data << "\n";

if (part_body.eof) {
break;
}
}

resp.set_status_and_content(status_type::ok, "multipart finished");
});
server.start();
}
```
```
async_simple::coro::Lazy<void> test_upload() {
std::string uri = "http://example.com/";
std::string uri = "http://127.0.0.1:9001/form_data";
coro_http_client client{};
auto result = co_await client.async_upload(uri, "test", "yourfile.jpg");
print(result.status);
std::cout << "upload finished\n";

client.add_str_part("hello", "coro_http_client");
client.add_file_part("test", "yourfile.jpg");
result = co_await client.async_upload(uri);
result = co_await client.async_upload_multipart(uri);
print(result.status);
std::cout << "upload finished\n";
}
Expand Down
40 changes: 39 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,37 @@ async_simple::coro::Lazy<void> basic_usage() {
co_return;
});

server.set_http_handler<POST>(
"/form_data",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::multipart);
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
if (part_head.ec) {
co_return;
}

std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n"; // if form data, no filename

auto part_body = co_await multipart.read_part_body(boundary);
if (part_body.ec) {
co_return;
}

std::cout << part_body.data << "\n";

if (part_body.eof) {
break;
}
}

resp.set_status_and_content(status_type::ok, "multipart finished");
});

server.set_http_handler<GET>(
"/in_thread_pool",
[](coro_http_request &req,
Expand Down Expand Up @@ -343,7 +374,7 @@ async_simple::coro::Lazy<void> basic_usage() {
person_t person{};
server.set_http_handler<GET>("/person", &person_t::foo, person);

server.async_start();
server.sync_start();
std::this_thread::sleep_for(300ms); // wait for server start

coro_http_client client{};
Expand All @@ -357,6 +388,13 @@ async_simple::coro::Lazy<void> basic_usage() {
result = co_await client.async_get("/coro");
assert(result.status == 200);

client.add_str_part("hello", "form_data");
client.add_str_part("test", "value");
result =
co_await client.async_upload_multipart("http://127.0.0.1:9001/form_data");
assert(result.status == 200);
assert(result.resp_body == "multipart finished");

result = co_await client.async_get("/in_thread_pool");
assert(result.status == 200);

Expand Down
16 changes: 14 additions & 2 deletions include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,19 @@ class coro_http_connection

if (!response_.get_delay()) {
if (head_buf_.size()) {
// handle pipeling, only support GET and HEAD method now.
if (parser_.method()[0] != 'G' && parser_.method()[0] != 'H') {
if (type == content_type::multipart) {
response_.set_status_and_content(
status_type::not_implemented,
"mutipart handler not implemented or incorrect implemented");
co_await reply();
close();
CINATRA_LOG_ERROR
<< "mutipart handler not implemented or incorrect implemented"
<< ec.message();
break;
}
else if (parser_.method()[0] != 'G' && parser_.method()[0] != 'H') {
// handle pipeling, only support GET and HEAD method now.
response_.set_status_and_content(status_type::method_not_allowed,
"method not allowed");
co_await reply();
Expand Down Expand Up @@ -343,6 +354,7 @@ class coro_http_connection
co_return;
}
}
head_buf_.consume(head_buf_.size());
}
else {
handle_session_for_response();
Expand Down
43 changes: 38 additions & 5 deletions lang/english/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,50 @@ async_simple::coro::Lazy<void> test_async_client() {
```

#### upload(multipart) file
```cpp
void start_server() {
coro_http_server server(1, 9001);
server.set_http_handler<POST>(
"/form_data",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
assert(req.get_content_type() == content_type::multipart);
auto boundary = req.get_boundary();
multipart_reader_t multipart(req.get_conn());
while (true) {
auto part_head = co_await multipart.read_part_head();
if (part_head.ec) {
co_return;
}

std::cout << part_head.name << "\n";
std::cout << part_head.filename << "\n";// if form data, no filename

auto part_body = co_await multipart.read_part_body(boundary);
if (part_body.ec) {
co_return;
}

std::cout << part_body.data << "\n";

if (part_body.eof) {
break;
}
}

resp.set_status_and_content(status_type::ok, "multipart finished");
});
server.start();
}
```
```cpp
async_simple::coro::Lazy<void> test_upload() {
std::string uri = "http://example.com/";
std::string uri = "http://127.0.0.1:9001/form_data";
coro_http_client client{};
auto result = co_await client.async_upload(uri, "test", "yourfile.jpg");
print(result.status);
std::cout << "upload finished\n";

client.add_str_part("hello", "coro_http_client");
client.add_file_part("test", "yourfile.jpg");
result = co_await client.async_upload(uri);
result = co_await client.async_upload_multipart(uri);
print(result.status);
std::cout << "upload finished\n";
}
Expand Down
Loading