Skip to content

Latest commit

History

79 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

UVHttp

UVHttp is an asynchronous HTTP framework written in C. The goal of uvhttp is to extend the libuv, and to support a small number of HTTP features.

UVHttp will teaching you how to create a simple http server and http client, before this I was very difficult to find a support c language, HTTPS, asynchronous framework of the HTTP server and the client's open source code, so I try to learn and develop the UVHttp, I hope to help you.

Features

Cross platform
HTTP keep-alive
Non-blocking I/O
HTTPS
Server and client

Contact

QQ:12178761
Email:12178761@qq.com
如果您感觉对您有所帮助,please press star

Sample

staticvoiduvhttp_session_request(
uvhttp_sessionsession,
structuvhttp_message*request
)
{
structuvhttp_header*list=request->headers;
structuvhttp_header*end=0;
structuvhttp_header*iter=0;
#ifdefOUTPUT_DEUBGINFOprintf( "session request url: %s\n", request->uri);
printf( "session headers:\n");
#endifiter=uvhttp_headers_begin( list);
end=uvhttp_headers_end( list);
for ( ; iter!=end ; iter=iter->next ) {
#ifdefOUTPUT_DEUBGINFOprintf( "%s: %s\n", iter->field, iter->value);
#endif
}
}
staticvoiduvhttp_session_body_read(
uvhttp_sessionsession,
structuvhttp_chunkdata
)
{
#ifdefOUTPUT_DEUBGINFOprintf( "request body: %.*s\n", data.len, data.base);
#endif
}
#defineRESPONSE \
"HTTP/1.1 200 OK\r\n" \
"Content-Type: text/plain\r\n" \
"Content-Length: 11\r\n" \
"Connection: keep-alive\r\n" \
"\r\n" \
"Hello World"
voidsession_writed(
intstatus,
uvhttp_sessionsession,
void*user_data
)
{
uvhttp_session_abort( session);
}
staticvoiduvhttp_session_request_end(
intstatus,
uvhttp_sessionsession
)
{
structuvhttp_chunkresponse;
response.base=RESPONSE;
response.len=sizeof(RESPONSE) -1;
uvhttp_session_write( session, &response, 0, session_writed);
#ifdefOUTPUT_DEUBGINFOprintf( "session request end!\n");
#endif
}
staticvoiduvhttp_session_end(
uvhttp_sessionsession
)
{
uvhttp_serverserver=0;
uvhttp_session_get_info( session, UVHTTP_SESSION_INFO_USER_DATA, &server);
#ifdefOUTPUT_DEUBGINFOprintf( "session end!\n");
#endif
}
staticvoiduvhttp_server_session_new(
uvhttp_serverserver,
uvhttp_sessionsession
)
{
uvhttp_session_set_option( session, UVHTTP_SESSION_OPT_REQUEST_CB, uvhttp_session_request);
uvhttp_session_set_option( session, UVHTTP_SESSION_OPT_REQUEST_BODY_CB, uvhttp_session_body_read);
uvhttp_session_set_option( session, UVHTTP_SESSION_OPT_REQUEST_END_CB, uvhttp_session_request_end);
uvhttp_session_set_option( session, UVHTTP_SESSION_OPT_END_CB, uvhttp_session_end);
uvhttp_session_set_option( session, UVHTTP_SESSION_OPT_USER_DATA, server);
#ifdefOUTPUT_DEUBGINFOprintf( "new session created!\n");
#endif
}
intmain(intargc, char*argv[])
{
uvhttp_looploop=uvhttp_loop_new();
if ( loop) {
uvhttp_serverserver_ssl=uvhttp_server_new( loop);
uvhttp_serverserver=uvhttp_server_new( loop);
if ( server) {
uvhttp_server_set_option( server, UVHTTP_SRV_OPT_SESSION_NEW_CB, uvhttp_server_session_new);
if ( uvhttp_server_ip4_listen( server, "0.0.0.0", 8011) ==UVHTTP_OK) {
printf("http on 8011 success\n");
}
}
if ( server_ssl) {
uvhttp_server_set_option( server_ssl, UVHTTP_SRV_OPT_SSL, 1);
uvhttp_server_set_option( server_ssl, UVHTTP_SRV_OPT_SESSION_NEW_CB, uvhttp_server_session_new);
if ( uvhttp_server_ip4_listen( server_ssl, "0.0.0.0", 8012) ==UVHTTP_OK) {
printf("https on 8012 success\n");
}
}
uvhttp_run( loop);
uvhttp_server_delete( server);
uvhttp_server_delete( server_ssl);
uvhttp_loop_delete( loop);
}
return0;
}

About

基于libuv实现的http跨平台通讯库,A http library based on libuv and mbedtls, supported tls/ssl

Resources

Stars

30 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages