forked from hzane/libhttpsvr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_apis.cc
More file actions
55 lines (47 loc) · 2.46 KB
/
http_apis.cc
File metadata and controls
55 lines (47 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "../libhttpsvr/precomp.h"
#include "../libhttpsvr/common_defs.h"
#include "../libhttpsvr/tpio_defs.h"
#include "../libhttpsvr/tpio_http_apis.h"
#include "../libhttpsvr/http_apis.h"
#include "../libhttpsvr/http_env.h"
auto http_receive_request_context_new() -> tpio_context* {
return _tpio_context_create( http_env::default_request_buffer_size );
}
auto http_receive_http_request( tpio_handle tpio, handle_t q, http_receive_request_end callback ) -> void {
auto ctx = http_receive_request_context_new();
ctx->end_io = [tpio, q, callback]( tpio_context*ctx, uint32_t result, uintptr_t xfered ) {
auto req = (result == NO_ERROR || result == ERROR_MORE_DATA) ? (http_request)ctx->buffer : nullptr;
callback( req, result, xfered );
auto next_req = result == ERROR_MORE_DATA || result == NO_ERROR || result == ERROR_HANDLE_EOF ;
if ( next_req ) {
http_receive_http_request( tpio, q, callback );
}
};
tpio_http_receive_http_request( tpio, q, ctx );
}
auto http_receive_request_entity_body( tpio_handle tpio, handle_t q, http_id rid, uintptr_t bufsize, http_receive_request_entity_body_end callback ) ->void {
auto ctx = _tpio_context_create( bufsize );
ctx->end_io = [callback]( tpio_context* ctx, uint32_t result, uintptr_t xfered ) {
callback( ctx->buffer, result, xfered );
};
tpio_http_receive_request_entity_body( tpio, q, rid, ctx );
}
auto http_send_http_response( tpio_handle tpio, handle_t q, http_id rid, tpio_context* ctx, bool hasmore, http_send_response_end callback ) -> void {
ctx->end_io = [callback]( tpio_context*, uint32_t result, uintptr_t xfered ) {
callback( result, xfered );
}; // nothing should be done
tpio_http_send_http_response( tpio, q, rid, ctx, hasmore );
}
auto http_send_response_entity_body( tpio_handle tpio, handle_t q, http_id rid, tpio_context* ctx, bool hasmore, http_send_response_entity_body_end callback ) -> void {
ctx->end_io = [callback]( tpio_context*ctx, uint32_t result, uintptr_t xfered ) {
callback( ctx->buffer, result, xfered );
};
tpio_http_send_response_entity_body( tpio, q, rid, ctx, hasmore);
}
auto http_cancel_http_request( tpio_handle tpio, handle_t q, http_id rid, http_cancel_http_request_end callback ) ->void {
auto ctx = _tpio_context_create( 0 );
ctx->end_io = [callback, rid](tpio_context*, uint32_t result, uintptr_t) {
callback( rid, result);
};
tpio_http_cancel_http_request( tpio, q, rid, ctx );
}