/* This is useful example echoing request data in response. */ #include #include void list_cookies(struct magi_request *r) { struct magi_cookies *current = r->cookies; printf("

"); /* Iterate through all cookies in request to show them in body: */ for (current = r->cookies; current; current = current->next) { struct magi_cookie *c = ¤t->item; printf("Cookie with name [%s", c->name); if (c->data) { printf("] is [%s", c->data); } if (c->domain) { printf("] for domain [%s", c->domain); } if (c->path) { printf("] for path [%s", c->path); } printf("]
"); } printf("

"); } void list_params(struct magi_params *current) { printf("

"); /* Iterate through specified params to show them in body: */ for (; current; current = current->next) { struct magi_param *p = ¤t->item; printf("[%s] is [%s]
", p->name, p->data); } printf("

"); } void list_files(struct magi_request *r) { struct magi_files *current; printf("

"); /* Iterate through all field files in request to show them in body: */ for (current = r->files; current; current = current->next) { struct magi_file *f = ¤t->item; printf("[%s] was [%s] on clientside
", f->field, f->filename); } printf("

"); } void show_meta(struct magi_request *r) { printf("

I was called "); if (r->is_secure) { printf("securely "); } printf("with method [%s", r->method); if (r->host) { printf("] on server [%s", r->host); } if (r->script) { printf("] being script on [%s", r->script); } if (r->path) { printf("] with requested path [%s", r->path); } printf("]

"); } void response(struct magi_request *r) { magi_response_default(); /* Pass default headers and send body: */ printf("" "" "Echo" ""); printf("

Echo CGI Script

"); show_meta(r); printf("

Cookies:

"); list_cookies(r); printf("

URL Parameters:

"); list_params(r->head); printf("

Body Parameters:

"); list_params(r->body); printf("

Files:

"); list_files(r); printf(""); } int main() { struct magi_request request; magi_request_init(&request); /* Setting defaults. */ if (magi_parse(&request)) { /* If parsing was done successful */ response(&request); /* we need to response the request. */ } else { /* And display error overwise: */ magi_error_response(request.error); } magi_request_free(&request); /* Don't forget to free everything after. */ return 0; }