Exemple #1
0
void
test_response_set_headers(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_header(res, "AsDf-QwEr", "test");
    g_assert(g_hash_table_size(res->headers) == 1);
    GSList *tmp = g_hash_table_lookup(res->headers, "asdf-qwer");
    g_assert_cmpstr(tmp->data, ==, "test");
    balde_response_free(res);
}
Exemple #2
0
void
test_response_render_with_custom_mime_type(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_header(res, "content-type", "text/plain");
    GString *out = balde_response_render(res, TRUE);
    g_assert_cmpstr(out->str, ==,
        "Content-Type: text/plain\r\nContent-Length: 3\r\n\r\nlol");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Exemple #3
0
static balde_response_t*
create_response(bluster_gist_ctx_t *ctx)
{
    balde_response_t* response = balde_make_response("");
    balde_response_set_header(response, "x-powered-by", PACKAGE_STRING);
    balde_response_set_tmpl_var(response, "bluster_url", PACKAGE_URL);

    gchar *gist_datetime = g_date_time_format(ctx->datetime,
        "%Y-%m-%d %H:%M:%S GMT");
    balde_response_set_tmpl_var(response, "gist_datetime", gist_datetime);
    g_free(gist_datetime);

    return response;
}
Exemple #4
0
GString*
balde_app_main_loop(balde_app_t *app, balde_request_env_t *env,
    balde_response_render_t render, balde_http_exception_code_t *status_code)
{
    balde_request_t *request = NULL;
    balde_response_t *response = NULL;
    balde_response_t *error_response = NULL;
    gchar *endpoint = NULL;
    gboolean with_body = TRUE;
    GString *rv = NULL;

    // render startup error, if any
    if (app->error != NULL) {
        error_response = balde_make_response_from_exception(app->error);
        rv = render(error_response, with_body);
        if (status_code != NULL)
            *status_code = error_response->status_code;
        balde_response_free(error_response);

        // free env, because it should be free'd by main loop and will not be
        // used anymore.
        balde_request_env_free(env);

        return rv;
    }

    request = balde_make_request(app, env);

    with_body = ! (request->method & BALDE_HTTP_HEAD);

    for (GSList *tmp = app->priv->before_requests; tmp != NULL; tmp = g_slist_next(tmp)) {
        balde_before_request_t *hook = tmp->data;
        hook->before_request_func(app, request);

        if (app->error != NULL) {
            error_response = balde_make_response_from_exception(app->error);
            rv = render(error_response, with_body);
            if (status_code != NULL)
                *status_code = error_response->status_code;
            balde_response_free(error_response);
            g_clear_error(&app->error);
            return rv;
        }
    }

    balde_app_t *app_copy = balde_app_copy(app);

    // get the view
    endpoint = balde_dispatch_from_path(app_copy->priv->views, request->path,
        &(request->priv->view_args));
    if (endpoint == NULL) {  // no view found! :(
        balde_abort_set_error(app_copy, 404);
    }
    else {
        // validate http method
        balde_view_t *view = balde_app_get_view_from_endpoint(app_copy, endpoint);
        if (request->method & view->url_rule->method) {
            // answer OPTIONS automatically
            if (request->method == BALDE_HTTP_OPTIONS) {
                response = balde_make_response("");
                gchar *allow = balde_list_allowed_methods(view->url_rule->method);
                balde_response_set_header(response, "Allow", allow);
                g_free(allow);
            }
            // run the view
            else {
                response = view->view_func(app_copy, request);
            }
        }
        // method not allowed
        else {
            balde_abort_set_error(app_copy, 405);
        }
        g_free(endpoint);
    }

    balde_request_free(request);

    if (app_copy->error != NULL) {
        error_response = balde_make_response_from_exception(app_copy->error);
        rv = render(error_response, with_body);
        if (status_code != NULL)
            *status_code = error_response->status_code;
        balde_response_free(response);
        balde_response_free(error_response);
        balde_app_free(app_copy);
        return rv;
    }

    rv = render(response, with_body);
    if (status_code != NULL)
        *status_code = response->status_code;
    balde_response_free(response);
    balde_app_free(app_copy);

    return rv;
}