• 2015-05-07

    PHP decode C string-like encoded string

    Views: 17420 | No Comments

    To decode string encoded in C string-like to what it exactly was in memory, I write a PHP function.

    C string-like encoded string as like this:


    The PHP function, unescape C string.

    function unescape_c_str($str){
        $ret = '';
        $len = strlen($str);
        for($i=0; $i<$len; $i++){
            if($str[$i] != '\\'){
                $ret .= $str[$i];
                case 's':
                    $ret .= ' ';
                case 'a':
                    $ret .= "\a";
                case 'b':
                    $ret .= "\b";
                case 'f':
                    $ret .= "\f";
                case 'v':
                    $ret .= "\v";
                case 'r':
                    $ret .= "\r";
                case 'n':
                    $ret .= "\n";
                case 't':
                    $ret .= "\t";
                case '\\':
                    $ret .= "\\";
                case 'x':
                    $hex = substr($str, $i+1, 2);
                    $ret .= hex2bin($hex);
                    $i += 2;
                    $ret .= $str[$i];
        return $ret;
    Posted by ideawu at 2015-05-07 12:54:20
  • 2014-04-12

    Libevent HTTP server memory leak

    Views: 10310 | No Comments

    The c1000k comet/push server icomet has just fixed a bug of memory leak. This bug is caused by not pairing every call to evhttp_send_reply_start() with evhttp_send_reply_end().

    The icomet project use libevent’s HTTP server API to easily set up a comet server, when a client connected, it calls:

    evhttp_send_reply_start(req, HTTP_OK, "OK");

    To send chunked data. And after a few seconds(by configuration) the long-polling times out, it ends up the HTTP request by calling:


    But when the client interupts the request, the callback function registered by evhttp_connection_set_closecb() is called, and evhttp_send_reply_end() is not called, so libevent will never free that request, memory leaks!

    Posted by ideawu at 2014-04-12 12:04:55
  • 2013-10-17

    SSDB C++ client API supported

    Views: 21213 | 2 Comments

    I am pleased to announce, the SSDB C++ client API is now provided! Along with documentations!

    The API is very easy to use, compiling a program is as simple as

    g++ -o hello-ssdb libssdb.a hello-ssdb.cpp

    Where hello-ssdb.cpp is a SSDB client application.

    SSDB C++ API Doc: http://www.ideawu.com/ssdb/docs/cpp/

    Posted by ideawu at 2013-10-17 22:32:58 Tags: ,
  • 2013-09-17

    Notify libevent HTTP server on client close

    Views: 14750 | No Comments

    Although we can set a close callback on HTTP connection, but the HTTP server is not notified if client close unexpetedly, such as CTRL+C.

     evhttp_connection_set_closecb(req->evcon, on_close, NULL);

    Because libevent called bufferevent_disable(evon, EV_READ) to stop read on the connection, so the server would not know the client close by read() returns 0, or send() causes SIGPIPE.

    All you need to is to re-enable EV_READ on that connection:

    struct bufferevent *bev = evhttp_connection_get_bufferevent(req->evcon);
    bufferevent_enable(bev, EV_READ);
    Posted by ideawu at 2013-09-17 13:45:33 Tags:
  • 2013-09-11

    Libevent HTTP client example

    Views: 24164 | 4 Comments

    This is a simple HTTP client written in C with libevent2, the client makes a HTTP request to a web server, and print out the response HTML text. Here’s the codes:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <evhttp.h>
    #include <event2/event.h>
    #include <event2/http.h>
    #include <event2/bufferevent.h>
    void http_request_done(struct evhttp_request *req, void *arg){
        char buf[1024];
        int s = evbuffer_remove(req->input_buffer, &buf, sizeof(buf) - 1);
        buf[s] = '\0';
        printf("%s", buf);
        // terminate event_base_dispatch()
        event_base_loopbreak((struct event_base *)arg);
    int main(int argc, char **argv){
        struct event_base *base;
        struct evhttp_connection *conn;
        struct evhttp_request *req;
        base = event_base_new();
        conn = evhttp_connection_base_new(base, NULL, "", 8080);
        req = evhttp_request_new(http_request_done, base);
        evhttp_add_header(req->output_headers, "Host", "localhost");
        //evhttp_add_header(req->output_headers, "Connection", "close");
        evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/index.php?id=1");
        evhttp_connection_set_timeout(req->evcon, 600);
        return 0;
    Posted by ideawu at 2013-09-11 21:55:59 Tags:
|<<<1234>>>| 1/4 Pages, 16 Results.