コード例 #1
0
ファイル: requests.c プロジェクト: GnLWeB/balde
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;
}
コード例 #2
0
ファイル: check_wrappers.c プロジェクト: baldero/balde
void
test_parse_authorization(void)
{
    g_assert(balde_parse_authorization(NULL) == NULL);
    g_assert(balde_parse_authorization("") == NULL);
    g_assert(balde_parse_authorization("Bola afddsfsdfdsgfdg") == NULL);
    g_assert(balde_parse_authorization("Basic Ym9sYQ==") == NULL);  // bola
    balde_authorization_t *a = balde_parse_authorization("Basic Ym9sYTpndWRh");  // bola:guda
    g_assert(a != NULL);
    g_assert_cmpstr(a->username, ==, "bola");
    g_assert_cmpstr(a->password, ==, "guda");
    balde_authorization_free(a);
    a = balde_parse_authorization("Basic Ym9sYTo=");  // bola:
    g_assert(a != NULL);
    g_assert_cmpstr(a->username, ==, "bola");
    g_assert_cmpstr(a->password, ==, "");
    balde_authorization_free(a);
    a = balde_parse_authorization("Basic Ym9sYTpndWRhOmxvbA==");  // bola:guda:lol
    g_assert(a != NULL);
    g_assert_cmpstr(a->username, ==, "bola");
    g_assert_cmpstr(a->password, ==, "guda:lol");
    balde_authorization_free(a);
}