Ejemplo n.º 1
0
balde_request_t*
balde_make_request(balde_app_t *app, balde_request_env_t *request_env)
{
    balde_request_t *request = g_new(balde_request_t, 1);
    request->priv = g_new(struct _balde_request_private_t, 1);
    request->priv->view_args = NULL;
    request->priv->body = NULL;
    request->priv->form = NULL;
    request->priv->files = NULL;
    request->priv->session = NULL;
    balde_request_env_t *env = request_env;
    if (request_env == NULL)
        env = balde_cgi_parse_request(app);
    request->path = env->path_info;
    request->server_name = env->server_name;
    request->script_name = env->script_name;
    if (env->path_info == NULL && env->script_name != NULL) {  // dumb webservers :/
        request->path = env->script_name;
        request->script_name = NULL;
    }
    request->method = balde_http_method_str2enum(env->request_method);
    request->https = env->https;
    request->priv->headers = env->headers;
    request->priv->args = balde_parse_query_string(env->query_string);
    request->priv->cookies = balde_parse_cookies(
        balde_request_get_header(request, "cookie"));
    request->authorization = balde_parse_authorization(
        balde_request_get_header(request, "authorization"));
    if (request->method & (BALDE_HTTP_POST | BALDE_HTTP_PUT | BALDE_HTTP_PATCH)) {
        request->priv->body = env->body;
        const gchar *ct = g_hash_table_lookup(request->priv->headers, "content-type");
        if (ct != NULL && g_str_has_prefix(ct, "multipart/form-data;")) {
            gchar *boundary = balde_multipart_parse_boundary(ct);
            balde_multipart_data_t *data = balde_multipart_parse(boundary,
                request->priv->body);
            g_free(boundary);
            if (data != NULL) {
                request->priv->files = data->files;
                request->priv->form = data->form;
            }
            g_free(data);
        }
        else {
            gchar *tmp = NULL;
            if (request->priv->body != NULL)
                tmp = request->priv->body->str;
            request->priv->form = balde_parse_query_string(tmp);
        }
    }
    g_free(env->query_string);
    g_free(env->request_method);
    g_free(env);
    return request;
}
Ejemplo n.º 2
0
void
test_request_get_arg(void)
{
    g_setenv("QUERY_STRING", "lol=hehe", TRUE);
    // FIXME: this thing is too weak :(
    balde_app_t *app = balde_app_init();
    balde_request_t *request = balde_make_request(app);
    g_assert_cmpstr(balde_request_get_arg(request, "lol"), == , "hehe");
    g_assert(balde_request_get_header(request, "xd") == NULL);
    balde_request_free(request);
    balde_app_free(app);
}
Ejemplo n.º 3
0
void
test_request_get_header(void)
{
    g_setenv("HTTP_LOL_HEHE", "12345", TRUE);
    g_setenv("HTTP_XD_KKK", "asdf", TRUE);
    // FIXME: this thing is too weak :(
    balde_app_t *app = balde_app_init();
    balde_request_t *request = balde_make_request(app);
    g_assert_cmpstr(balde_request_get_header(request, "Lol-Hehe"), ==, "12345");
    g_assert_cmpstr(balde_request_get_header(request, "XD-KKK"), ==, "asdf");
    g_assert(balde_request_get_header(request, "foo") == NULL);
    balde_request_free(request);
    balde_app_free(app);
}