|
|
|
#include "../../include/rapida.h"
|
|
|
|
#include "../../include/servers/tcp.h"
|
|
|
|
#include <string.h> /* for strdup() */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \brief Process home page request.
|
|
|
|
*
|
|
|
|
* \param req Request.
|
|
|
|
* \param res Response.
|
|
|
|
* \param userdata Additional data that you can pass to this handler
|
|
|
|
* when adding a route.
|
|
|
|
*/
|
|
|
|
static void home_page_handler(rpd_req *req, rpd_res *res, void *userdata)
|
|
|
|
{
|
|
|
|
/* Check request method */
|
|
|
|
switch (req->method) {
|
|
|
|
case HEAD:
|
|
|
|
res->status = rpd_res_st_ok;
|
|
|
|
rpd_keyval_insert(&res->headers, "Content-Type", "text/plain");
|
|
|
|
break;
|
|
|
|
case GET:
|
|
|
|
res->status = rpd_res_st_ok;
|
|
|
|
rpd_keyval_insert(&res->headers, "Content-Type", "text/plain");
|
|
|
|
rpd_keyval_insert(&res->headers, "JavaScript", "Sucks");
|
|
|
|
|
|
|
|
/* Please allocate body on the heap,
|
|
|
|
* because after calling this handler
|
|
|
|
* Rapida will free it.
|
|
|
|
*/
|
|
|
|
res->body = strdup("Hello World!");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Other request methods are not implemented,
|
|
|
|
* so set the appropriate response status.
|
|
|
|
*/
|
|
|
|
res->status = rpd_res_st_not_implemented;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* You don't have to do something like send a response.
|
|
|
|
* Rapida will do this after calling this handler.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
rpd_app app;
|
|
|
|
/* Initialize application. */
|
|
|
|
rpd_app_create(&app);
|
|
|
|
|
|
|
|
/* Add home "/" page handler. */
|
|
|
|
rpd_app_add_route(&app, "/", &home_page_handler, NULL);
|
|
|
|
|
|
|
|
/* Run the application and return its status code. */
|
|
|
|
return rpd_tcp_server_start(&app, "http://localhost:8080");
|
|
|
|
}
|