Libevhtp: Difference between revisions
(Created page with "== Overview == == References == <references /> category:libevent") |
No edit summary |
||
Line 1: | Line 1: | ||
== Overview == | == Overview == | ||
libevhtp 는 libevent 를 이용한 HTTP API 라이브러리이다. libevhtp 를 사용하면 손쉽게 REST 인터페이스를 구현할 수 있다. | |||
홈페이지: https://github.com/ellzey/libevhtp | |||
== Functions == | |||
=== 생성/삭제 === | |||
<source lang=c> | |||
/** | |||
* @brief creates a new evhtp_t instance | |||
* | |||
* @param evbase the initialized event base | |||
* @param arg user-defined argument which is evhtp_t specific | |||
* | |||
* @return a new evhtp_t structure or NULL on error | |||
*/ | |||
evhtp_t * evhtp_new(evbase_t * evbase, void * arg); | |||
void evhtp_free(evhtp_t * evhtp); | |||
</source> | |||
evhtp 인스턴스 생성/삭제 함수 | |||
=== 콜백 등록 === | |||
* Interface | |||
<source lang=c> | |||
/** | |||
* @brief sets a callback to be executed on a specific path | |||
* | |||
* @param htp the initialized evhtp_t | |||
* @param path the path to match | |||
* @param cb the function to be executed | |||
* @param arg user-defined argument passed to the callback | |||
* | |||
* @return evhtp_callback_t * on success, NULL on error. | |||
*/ | |||
evhtp_callback_t * evhtp_set_cb(evhtp_t * htp, const char * path, evhtp_callback_cb cb, void * arg); | |||
</source> | |||
지정된 경로가 호출되었을 때 실행 될 콜백 함수를 지정한다. | |||
* Example | |||
<source lang=c> | |||
void testcb(evhtp_request_t *req, void *a) | |||
{ | |||
const char *str = a; | |||
evbuffer_add_printf(req->buffer_out, "%s", str); | |||
evhtp_send_reply(req, EVHTP_RES_OK); | |||
} | |||
evhtp_t *htp = evhtp_new(evbase, NULL); | |||
evhtp_set_cb(htp, "/simple/", testcb, "simple"); | |||
</source> | |||
=== Listen 소켓 설정 === | |||
* Interface | |||
<source lang=c> | |||
/** | |||
* @brief bind to a socket, optionally with specific protocol support | |||
* formatting. The addr can be defined as one of the following: | |||
* ipv6:<ipv6addr> for binding to an IPv6 address. | |||
* unix:<named pipe> for binding to a unix named socket | |||
* ipv4:<ipv4addr> for binding to an ipv4 address | |||
* Otherwise the addr is assumed to be ipv4. | |||
* | |||
* @param htp | |||
* @param addr | |||
* @param port | |||
* @param backlog | |||
* | |||
* @return | |||
*/ | |||
int evhtp_bind_socket(evhtp_t * htp, const char * addr, uint16_t port, int backlog); | |||
</source> | |||
Listen 주소를 설정한다. 주소 설정 시, 자동으로 주소 타입을 파싱해서 알맞은 소켓 타입으로 생성한 후, listen 한다. "0.0.0.0" 설정시, localhost 주소를 listen 한다. | |||
* Example | |||
<source lang=c> | |||
evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024); | |||
</source> | |||
== Samples == | |||
=== test_basic === | |||
* test_basic.c | |||
<source lang=c> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <stdint.h> | |||
#include <errno.h> | |||
#include <evhtp.h> | |||
void testcb(evhtp_request_t *req, void *a) | |||
{ | |||
const char *str = a; | |||
evbuffer_add_printf(req->buffer_out, "%s", str); | |||
evhtp_send_reply(req, EVHTP_RES_OK); | |||
} | |||
int main(int argc, char **argv) | |||
{ | |||
evbase_t *evbase = event_base_new(); | |||
evhtp_t *htp = evhtp_new(evbase, NULL); | |||
evhtp_set_cb(htp, "/simple/", testcb, "simple"); | |||
evhtp_set_cb(htp, "/1/ping", testcb, "one"); | |||
evhtp_set_cb(htp, "/1/ping.json", testcb, "two"); | |||
#ifndef EVHTP_DISABLE_EVTHR | |||
evhtp_use_threads(htp, NULL, 4, NULL); | |||
#endif | |||
evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024); | |||
event_base_loop(evbase, 0); | |||
evhtp_unbind_socket(htp); | |||
evhtp_free(htp); | |||
event_base_free(evbase); | |||
return 0; | |||
} | |||
</source> | |||
== References == | == References == |
Revision as of 15:24, 4 February 2015
Overview
libevhtp 는 libevent 를 이용한 HTTP API 라이브러리이다. libevhtp 를 사용하면 손쉽게 REST 인터페이스를 구현할 수 있다.
홈페이지: https://github.com/ellzey/libevhtp
Functions
생성/삭제
<source lang=c> /**
* @brief creates a new evhtp_t instance * * @param evbase the initialized event base * @param arg user-defined argument which is evhtp_t specific * * @return a new evhtp_t structure or NULL on error */
evhtp_t * evhtp_new(evbase_t * evbase, void * arg); void evhtp_free(evhtp_t * evhtp); </source> evhtp 인스턴스 생성/삭제 함수
콜백 등록
- Interface
<source lang=c> /**
* @brief sets a callback to be executed on a specific path * * @param htp the initialized evhtp_t * @param path the path to match * @param cb the function to be executed * @param arg user-defined argument passed to the callback * * @return evhtp_callback_t * on success, NULL on error. */
evhtp_callback_t * evhtp_set_cb(evhtp_t * htp, const char * path, evhtp_callback_cb cb, void * arg); </source> 지정된 경로가 호출되었을 때 실행 될 콜백 함수를 지정한다.
- Example
<source lang=c> void testcb(evhtp_request_t *req, void *a) {
const char *str = a; evbuffer_add_printf(req->buffer_out, "%s", str); evhtp_send_reply(req, EVHTP_RES_OK);
}
evhtp_t *htp = evhtp_new(evbase, NULL);
evhtp_set_cb(htp, "/simple/", testcb, "simple"); </source>
Listen 소켓 설정
- Interface
<source lang=c> /**
* @brief bind to a socket, optionally with specific protocol support * formatting. The addr can be defined as one of the following: * ipv6:<ipv6addr> for binding to an IPv6 address. * unix:<named pipe> for binding to a unix named socket * ipv4:<ipv4addr> for binding to an ipv4 address * Otherwise the addr is assumed to be ipv4. * * @param htp * @param addr * @param port * @param backlog * * @return */
int evhtp_bind_socket(evhtp_t * htp, const char * addr, uint16_t port, int backlog); </source> Listen 주소를 설정한다. 주소 설정 시, 자동으로 주소 타입을 파싱해서 알맞은 소켓 타입으로 생성한 후, listen 한다. "0.0.0.0" 설정시, localhost 주소를 listen 한다.
- Example
<source lang=c> evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024); </source>
Samples
test_basic
- test_basic.c
<source lang=c>
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
- include <stdint.h>
- include <errno.h>
- include <evhtp.h>
void testcb(evhtp_request_t *req, void *a) {
const char *str = a; evbuffer_add_printf(req->buffer_out, "%s", str); evhtp_send_reply(req, EVHTP_RES_OK);
}
int main(int argc, char **argv) {
evbase_t *evbase = event_base_new(); evhtp_t *htp = evhtp_new(evbase, NULL); evhtp_set_cb(htp, "/simple/", testcb, "simple"); evhtp_set_cb(htp, "/1/ping", testcb, "one"); evhtp_set_cb(htp, "/1/ping.json", testcb, "two");
- ifndef EVHTP_DISABLE_EVTHR
evhtp_use_threads(htp, NULL, 4, NULL);
- endif
evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024); event_base_loop(evbase, 0); evhtp_unbind_socket(htp); evhtp_free(htp); event_base_free(evbase); return 0;
} </source>
References
<references />