コード例 #1
0
// Swap this with main -- this is a protocol test
int test(int argc, char **args){
    /**
     * Example from page 8 of the web socket protocol RFC : http://www.whatwg.org/specs/web-socket-protocol/
     */
    char *headers = "{\"PATH\":\"/dds_stream\",\"host\":\"localhost:6767\","
                    "\"sec-websocket-key1\":\"18x 6]8vM;54 *(5:  {   U1]8  z [  8\","
                    "\"origin\":\"http://localhost:6767\",\"x-forwarded-for\":\"::1\","
                    "\"upgrade\":\"WebSocket\",\"connection\":\"Upgrade\","
                    "\"sec-websocket-key2\":\"1_ tx7X d  <  nw  334J702) 7]o}` 0\","
                    "\"METHOD\":\"GET\",\"VERSION\":\"HTTP/1.1\",\"URI\":\"/dds_stream\","
                    "\"PATTERN\":\"/dds_stream\"}";
    char *body = "Tm[K T2u";
    mongrel2_request *req = calloc(1,sizeof(mongrel2_request));
    req->raw_headers = bfromcstr(headers);
    req->body = bfromcstr(body);
    unsigned char response[16];
    mongrel2_ws_handshake_response(req, response);
    if(strncmp((const char*)response,"fQJ,fN/4F4!~K~MH",16) != 0){
        fprintf(stdout,"Response did not match expection\n");
        fprintf(stdout,"Exepcted: fQJ,fN/4F4!~K~MH\n");
        fprintf(stdout,"Got: %s",response);
    }
    mongrel2_request_finalize(req);
    return 0;
}
コード例 #2
0
ファイル: body_toupper.c プロジェクト: amuraru/libm2handler
int main(int argc, char **args){
    char* SENDER = NULL;

    if (argc > 1 ) {
        SENDER = args[1];
    }
    printf("Handler ID : %s\n", SENDER);
    bstring pull_addr = bfromcstr("tcp://127.0.0.1:9999");
    bstring pub_addr  = bfromcstr("tcp://127.0.0.1:9998");

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx);
    mongrel2_connect(pull_socket, bdata(pull_addr));

    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));
    if (SENDER != NULL) {
        mongrel2_set_identity(pub_socket, SENDER);
    }

    const bstring headers = bfromcstr("HTTP/1.1 200 OK\r\nDate: Fri, 07 Jan 2011 01:15:42 GMT\r\nStatus: 200 OK\r\nConnection: close");
    mongrel2_request *request;

    while(1){
        request = mongrel2_recv(pull_socket);
        btoupper(request->body);
        mongrel2_reply_http(pub_socket, request, headers, request->body);
        mongrel2_disconnect(pub_socket, request);
        mongrel2_request_finalize(request);
    }

    bdestroy(headers);

    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    return 0;
}
コード例 #3
0
ファイル: fifo_reader.c プロジェクト: xrl/libm2handler
int main(int argc, char **args){
    bstring pull_addr = bfromcstr("tcp://127.0.0.1:8999");
    bstring pub_addr  = bfromcstr("tcp://127.0.0.1:8998");

    mongrel2_ctx *ctx = mongrel2_init(1); // Yes for threads?

    mongrel2_socket *pull_socket = mongrel2_pull_socket(ctx,bdata(&SENDER));
    mongrel2_connect(pull_socket, bdata(pull_addr));

    mongrel2_socket *pub_socket = mongrel2_pub_socket(ctx);
    mongrel2_connect(pub_socket, bdata(pub_addr));

    const bstring headers = bfromcstr("HTTP/1.1 200 OK\r\nDate: Fri, 07 Jan 2011 01:15:42 GMT\r\nStatus: 200 OK\r\nConnection: close");
    mongrel2_request *request;

    // Need to set umask to allow file creation... then restore? Not sure!
    mode_t process_mask = umask(0);
    int retval;
    retval = mkfifo("handler_pipe", S_IRUSR);
    umask(process_mask);
    if(retval != 0){
        switch(errno){
            case EACCES :
                fprintf(stderr,"Insufficient permissions to create fifo handler_pipe");
            case EEXIST :
                //fprintf(stderr,"handler_pipe already exists. That's cool tho.");
                goto no_mkfifo_error;
            case ENAMETOOLONG :
                fprintf(stderr,"handler_pipe is too long of a name. Unlikely!");
                break;
            case ENOENT :
                fprintf(stderr,"path for handler_pipe does not exist");
                break;
            case ENOSPC :
                fprintf(stderr,"no more room for handler_pipe");
                break;
            case ENOTDIR :
                fprintf(stderr,"path for handler_pipe is not a directory");
                break;
            case EROFS :
                fprintf(stderr,"handler_pipe cannot be created on read only fs");
                break;
            default:
                fprintf(stderr,"Error creating fifo. Not sure what though. Sorry! %d",errno);
                break;
        }
        exit(EXIT_FAILURE);
    }
    no_mkfifo_error:
    fprintf(stdout,"Created handler_pipe AOK\n");
    
    FILE *fifofd = fopen("handler_pipe","r");
    if(fifofd == NULL){
        fprintf(stderr,"Could not open handler_pipe");
        exit(EXIT_FAILURE);
    }

    size_t read_size;
    request = mongrel2_recv(pull_socket);
    // A 1k buffer
    void* fifo_buffer = calloc(1024,1);
    //while(1){
        read_size = fread(fifo_buffer, 1024, 1, fifofd);
        fprintf(stdout,"read_size from fifo: %zd", read_size); // z is for size_t's... weird!
        bdestroy(request->body);
        request->body = bfromcstralloc(1024,(const char*)fifo_buffer);
        mongrel2_reply_http(pub_socket, request, headers, request->body);
        mongrel2_disconnect(pub_socket, request);
    //}
    mongrel2_request_finalize(request);

    bdestroy(headers);

    bdestroy(pull_addr);
    bdestroy(pub_addr);

    mongrel2_close(pull_socket);
    mongrel2_close(pub_socket);
    mongrel2_deinit(ctx);
    return 0;
}