示例#1
0
static ladders_rec_t *
init_reqrec(lwan_request_t *request, lwan_response_t *response)
{
    ladders_rec_t *rec = NULL;
    static const char errmsg[] = "{\"error\":\"bad parameter\"}";
    const char *start = lwan_request_get_query_param(request, "start");
    const char *goal = lwan_request_get_query_param(request, "goal");

    if (!start || !goal) {
        strbuf_set_static(response->buffer, errmsg, sizeof(errmsg) - 1);
        return NULL;
    }

    strbuf_append_printf(response->buffer,
                         "{\"start\":\"%s\",\"goal\":\"%s\",",
                         start, goal);

    if (start[5] != 0 || strlen(start) < 5 || 
        goal[5] != 0 || strlen(goal) < 5) {
        strbuf_append_str(response->buffer,
                          "\"path\":[],\"error\":\"Paramter start and "
                          "goal should be five-letter words.\"}", 0);
        return NULL;
    }

    rec = malloc(sizeof(*rec));
    strcpy(rec->start, start);
    strcpy(rec->goal, goal);
    rec->response = response;
    strbuf_init(&rec->path);

    return rec;
}
示例#2
0
文件: lwan-lua.c 项目: Abioy/lwan
static int req_say_cb(lua_State *L)
{
    lwan_request_t *request = userdata_as_request(L, 1);
    size_t response_str_len;
    const char *response_str = lua_tolstring(L, -1, &response_str_len);

    strbuf_set_static(request->response.buffer, response_str, response_str_len);
    lwan_response_send_chunk(request);

    return 0;
}
示例#3
0
文件: lwan-lua.c 项目: Abioy/lwan
static int req_send_event_cb(lua_State *L)
{
    lwan_request_t *request = userdata_as_request(L, 1);
    size_t event_str_len;
    const char *event_str = lua_tolstring(L, -1, &event_str_len);
    const char *event_name = lua_tostring(L, -2);

    strbuf_set_static(request->response.buffer, event_str, event_str_len);
    lwan_response_send_event(request, event_name);

    return 0;
}