Beispiel #1
0
balde_response_t*
home(balde_app_t *app, balde_request_t *request)
{
    balde_response_t *response = balde_make_response("");
    balde_template_url_for(app, request, response);
    return response;
}
Beispiel #2
0
void
test_response_get_tmpl_var(void)
{
    balde_response_t *res = balde_make_response("lol");
    g_hash_table_insert(res->template_ctx, g_strdup("bola"), g_strdup("guda"));
    g_assert_cmpstr(balde_response_get_tmpl_var(res, "bola"), ==, "guda");
    balde_response_free(res);
}
Beispiel #3
0
void
test_response_append_body_len(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_append_body_len(res, "heheasd", 4);
    g_assert_cmpstr(res->body->str, ==, "lolhehe");
    balde_response_free(res);
}
Beispiel #4
0
void
test_response_set_cookie_with_secure(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, -1, NULL, NULL, TRUE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==, "bola=\"guda\"; Secure; Path=/");
    balde_response_free(res);
}
Beispiel #5
0
void
test_response_set_cookie_with_domain(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, -1, NULL, "bola.com", FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==, "bola=\"guda\"; Domain=\"bola.com\"; Path=/");
    balde_response_free(res);
}
Beispiel #6
0
void
test_response_set_tmpl_var(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_tmpl_var(res, "bola", "guda");
    g_assert(g_hash_table_size(res->template_ctx) == 1);
    g_assert_cmpstr(g_hash_table_lookup(res->template_ctx, "bola"), ==, "guda");
    balde_response_free(res);
}
Beispiel #7
0
balde_response_t*
hello(balde_app_t *app, balde_request_t *request)
{
    balde_response_t *response = balde_make_response("");
    const gchar *name = balde_request_get_arg(request, "name");
    balde_response_set_tmpl_var(response, "name", name != NULL ? name : "World");
    balde_template_hello(app, request, response);
    return response;
}
Beispiel #8
0
balde_response_t*
profile(balde_app_t *app, balde_request_t *request)
{
    const gchar *name = balde_request_get_view_arg(request, "name");
    gchar *str = g_strdup_printf("Hello, %s\n", name != NULL ? name : "World");
    balde_response_t *response = balde_make_response(str);
    g_free(str);
    return response;
}
Beispiel #9
0
void
test_response_delete_cookie(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_delete_cookie(res, "bola", NULL, NULL);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"\"; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/");
    balde_response_free(res);
}
Beispiel #10
0
void
test_response_set_cookie_with_expires_and_max_age(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", 60, 1235555555, NULL, NULL, FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"guda\"; Expires=Wed, 25-Feb-2009 09:52:35 GMT; Max-Age=60; Path=/");
    balde_response_free(res);
}
Beispiel #11
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);
}
Beispiel #12
0
void
test_response_render_without_body(void)
{
    balde_response_t *res = balde_make_response("lol");
    GString *out = balde_response_render(res, FALSE);
    g_assert_cmpstr(out->str, ==,
        "Content-Type: text/html; charset=utf-8\r\nContent-Length: 3\r\n\r\n");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Beispiel #13
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);
}
Beispiel #14
0
void
test_response_set_cookie_with_expires(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", -1, 1234567890, NULL, NULL,
        FALSE);
    GSList *tmp = g_hash_table_lookup(res->headers, "set-cookie");
    g_assert_cmpstr(tmp->data, ==,
        "bola=\"guda\"; Expires=Fri, 13-Feb-2009 23:31:30 GMT; Path=/");
    balde_response_free(res);
}
Beispiel #15
0
void
test_make_response(void)
{
    balde_response_t *res = balde_make_response("lol");
    g_assert(res != NULL);
    g_assert(res->status_code == 200);
    g_assert(g_hash_table_size(res->headers) == 0);
    g_assert(g_hash_table_size(res->template_ctx) == 0);
    g_assert_cmpstr(res->body->str, ==, "lol");
    balde_response_free(res);
}
Beispiel #16
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;
}
Beispiel #17
0
balde_response_t*
hello(balde_app_t *app, balde_request_t *request)
{
    balde_response_t *response = balde_make_response("");
    if (request->method == BALDE_HTTP_POST) {
        const gchar *name = balde_request_get_form(request, "name");
        balde_response_set_tmpl_var(response, "name",
            name != NULL && name[0] != '\0' ? name : "World");
        balde_template_hello(app, request, response);
    }
    else
        balde_template_form(app, request, response);
    return response;
}
Beispiel #18
0
void
test_response_render_with_multiple_cookies(void)
{
    balde_response_t *res = balde_make_response("lol");
    balde_response_set_cookie(res, "bola", "guda", 60, -1, NULL, NULL, FALSE);
    balde_response_set_cookie(res, "asd", "qwe", -1, -1, NULL, NULL, FALSE);
    balde_response_set_cookie(res, "xd", ":D", -1, -1, "/bola/", NULL, FALSE);
    GString *out = balde_response_render(res, TRUE);
    g_assert_cmpstr(out->str, ==,
        "Set-Cookie: bola=\"guda\"; Expires=Fri, 13-Feb-2009 23:32:30 GMT; Max-Age=60; Path=/\r\n"
        "Set-Cookie: asd=\"qwe\"; Path=/\r\n"
        "Set-Cookie: xd=\":D\"; Path=/bola/\r\n"
        "Content-Type: text/html; charset=utf-8\r\n"
        "Content-Length: 3\r\n"
        "\r\n"
        "lol");
    g_string_free(out, TRUE);
    balde_response_free(res);
}
Beispiel #19
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;
}