Libevhtp: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:


홈페이지: https://github.com/ellzey/libevhtp
홈페이지: https://github.com/ellzey/libevhtp
== Types ==
=== 이벤트 콜백 ===
<source lang=c>
typedef struct evhtp_callback_s  evhtp_callback_t;
/**
* @brief structure containing a single callback and configuration
*
* The definition structure which is used within the evhtp_callbacks_t
* structure. This holds information about what should execute for either
* a single or regex path.
*
* For example, if you registered a callback to be executed on a request
* for "/herp/derp", your defined callback will be executed.
*
* Optionally you can set callback-specific hooks just like per-connection
* hooks using the same rules.
*
*/
struct evhtp_callback_s {
    evhtp_callback_type type;          /**< the type of callback (regex|path) */
    evhtp_callback_cb  cb;            /**< the actual callback function */
    unsigned int        hash;          /**< the full hash generated integer */
    void              * cbarg;          /**< user-defind arguments passed to the cb */
    evhtp_hooks_t    * hooks;          /**< per-callback hooks */
    union {
        char * path;
        char * glob;
#ifndef EVHTP_DISABLE_REGEX
        regex_t * regex;
#endif
    } val;
    TAILQ_ENTRY(evhtp_callback_s) next;
};
</source>


== Functions ==
== Functions ==

Revision as of 09:53, 5 February 2015

Overview

libevhtp 는 libevent 를 이용한 HTTP API 라이브러리이다. libevhtp 를 사용하면 손쉽게 REST 인터페이스를 구현할 수 있다.

홈페이지: https://github.com/ellzey/libevhtp

Types

이벤트 콜백

<source lang=c> typedef struct evhtp_callback_s evhtp_callback_t;

/**

* @brief structure containing a single callback and configuration
*
* The definition structure which is used within the evhtp_callbacks_t
* structure. This holds information about what should execute for either
* a single or regex path.
*
* For example, if you registered a callback to be executed on a request
* for "/herp/derp", your defined callback will be executed.
*
* Optionally you can set callback-specific hooks just like per-connection
* hooks using the same rules.
*
*/

struct evhtp_callback_s {

   evhtp_callback_type type;           /**< the type of callback (regex|path) */
   evhtp_callback_cb   cb;             /**< the actual callback function */
   unsigned int        hash;           /**< the full hash generated integer */
   void              * cbarg;          /**< user-defind arguments passed to the cb */
   evhtp_hooks_t     * hooks;          /**< per-callback hooks */
   union {
       char * path;
       char * glob;
  1. ifndef EVHTP_DISABLE_REGEX
       regex_t * regex;
  1. endif
   } val;
   TAILQ_ENTRY(evhtp_callback_s) next;

}; </source>

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);

/**

* @brief stops the listening socket.
*
* @param htp
*/

void evhtp_unbind_socket(evhtp_t * htp); </source> Listen 주소를 설정/해제한다. 주소 설정 시, 자동으로 주소 타입을 파싱해서 알맞은 소켓 타입으로 생성한 후, listen 한다. "0.0.0.0" 설정시, localhost 주소를 listen 한다.

  • Example

<source lang=c> evhtp_bind_socket(htp, "0.0.0.0", 8081, 1024);

evhtp_unbind_socket(htp); </source>

Samples

test_basic

  • test_basic.c

<source lang=c>

  1. include <stdio.h>
  2. include <stdlib.h>
  3. include <string.h>
  4. include <stdint.h>
  5. include <errno.h>
  6. 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");
  1. ifndef EVHTP_DISABLE_EVTHR
   evhtp_use_threads(htp, NULL, 4, NULL);
  1. 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 />