libevhtp

示例

//
//  main.c
//  test-evhtp
//
//  Created by Zhibiao Pan on 12-2-21.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//
//  build:
//    gcc -o evhtpd  -I/usr/local/include -L/usr/local/lib -levhtp 
//        -levent -levent_openssl  -L/usr/local/ssl/lib -lssl -lcrypto main.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);
 
void
testcb(evhtp_request_t * req, void * a) {
  evbuffer_add_reference(req->buffer_out, "foobar", 6, NULL, NULL);
  evhtp_send_reply(req, EVHTP_RES_OK);
}
 
 
int main (int argc, const char * argv[])
{
  evbase_t * evbase = event_base_new();
  evhtp_t  * htp    = evhtp_new(evbase, NULL);
 
  evhtp_set_cb(htp, "/", testcb, NULL);
  evhtp_use_threads(htp, NULL, 4, NULL);
  evhtp_bind_socket(htp, "0.0.0.0", 8080, 1024);
  event_base_loop(evbase, 0);
 
  return 0;
}

获取远程IP

char ip[INET_ADDRSTRLEN] = {0};
 
unsigned char *addr = (unsigned char *)&(((struct sockaddr_in *)(req->conn->saddr))->sin_addr);
sprintf(ip, "%d.%d.%d.%d",
        addr[0] & 0xff, addr[1] & 0xff,
        addr[2] & 0xff, addr[3] & 0xff);
 

数据结构

/**
 * @brief a generic container representing an entire URI strucutre
 */
struct evhtp_uri_s {
    evhtp_authority_t * authority;
    evhtp_path_t      * path;
    unsigned char     * fragment;     /**< data after '#' in uri */
    unsigned char     * query_raw;    /**< the unparsed query arguments */
    evhtp_query_t     * query;        /**< list of k/v for query arguments */
    htp_scheme          scheme;       /**< set if a scheme is found */
};
 
 
/**
 * @brief structure which represents authority information in a URI
 */
struct evhtp_authority_s {
    char   * username;                /**< the username in URI (scheme://USER:.. */
    char   * password;                /**< the password in URI (scheme://...:PASS.. */
    char   * hostname;                /**< hostname if present in URI */
    uint16_t port;                    /**< port if present in URI */
};
 
 
/**
 * @brief structure which represents a URI path and or file
 */
struct evhtp_path_s {
    char       * full;                /**< the full path+file (/a/b/c.html) */
    char       * path;                /**< the path (/a/b/) */
    char       * file;                /**< the filename if present (c.html) */
    char       * match_start;
    char       * match_end;
    unsigned int matched_soff;        /**< offset of where the uri starts
                                       *   mainly used for regex matching
                                       */
    unsigned int matched_eoff;        /**< offset of where the uri ends
                                       *   mainly used for regex matching
                                       */
};

常用函数

获取HTTP请求方法

enum htp_method {
    htp_method_GET = 0,
    htp_method_HEAD,
    htp_method_POST,
    htp_method_PUT,
    htp_method_DELETE,
    htp_method_MKCOL,
    htp_method_COPY,
    htp_method_MOVE,
    htp_method_OPTIONS,
    htp_method_PROPFIND,
    htp_method_PROPPATCH,
    htp_method_LOCK,
    htp_method_UNLOCK,
    htp_method_TRACE,
    htp_method_UNKNOWN
};
htp_method evhtp_request_get_method(evhtp_request_t * r);
/**
 * @brief creates a new evhtp_header_t, sets only the key, and adds to the
 *        evhtp_headers TAILQ
 *
 * @param headers the evhtp_headers_t TAILQ (evhtp_kv_t)
 * @param key a null terminated string
 * @param kalloc if 1 the string will be copied, otherwise assigned
 *
 * @return an evhtp_header_t pointer or NULL on error
 */
evhtp_header_t * evhtp_header_key_add(evhtp_headers_t * headers, const char * key, char kalloc);
 
 
/**
 * @brief finds the last header in the headers tailq and adds the value
 *
 * @param headers the evhtp_headers_t TAILQ (evhtp_kv_t)
 * @param val a null terminated string
 * @param valloc if 1 the string will be copied, otherwise assigned
 *
 * @return an evhtp_header_t pointer or NULL on error
 */
evhtp_header_t * evhtp_header_val_add(evhtp_headers_t * headers, const char * val, char valloc);
 
/**
 * @brief Parses the query portion of the uri into a set of key/values
 *
 * Parses query arguments like "?herp=derp&foo=bar;blah=baz"
 *
 * @param query data containing the uri query arguments
 * @param len size of the data
 *
 * @return evhtp_query_t * on success, NULL on error
 */
evhtp_query_t * evhtp_parse_query(const char * query, size_t len);
linux/libevhtp.txt · 最后更改: 2012/03/18 21:38 由 admin
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki