Viewing file: demo4.c (1.59 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
/* * demo4.c: sample module showing URL-based redirection. */
#include <unistd.h>
#include "tuxmodule.h"
#ifdef TUXAPI_declare TUXAPI_declare; #endif
#define REPLY_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" #define REPLY_HEADER_LEN (sizeof(REPLY_HEADER)-1) #define ERROR "GET_OBJECT error.\n" #define ERROR_LEN (sizeof(ERROR)-1)
#define EXT ".shtml" #define EXT_LEN (sizeof(EXT)-1)
int TUXAPI_handle_events (user_req_t *req) { int ret = TUX_RETURN_USERSPACE_REQUEST; int len;
len = strlen(req->objectname); if ((len > EXT_LEN) && !memcmp(req->objectname + len - EXT_LEN, EXT, EXT_LEN)) return tux(TUX_ACTION_REDIRECT_REQ, req); if (!req->objectname[0] && !req->query[0]) return tux(TUX_ACTION_FINISH_CLOSE_REQ, req);
switch (req->event) { /* * A new request starts with event code 0. */ case 0: write (req->sock, REPLY_HEADER, REPLY_HEADER_LEN); req->http_status = 200;
/* * set req->objectname to the requested object * in the query string if it exists. This simulates * simple static GETs. Otherwise just send the * requested object. */ if (req->query[0]) strcpy(req->objectname, req->query);
req->event = 1; ret = tux(TUX_ACTION_GET_OBJECT, req); if (ret < 0 || req->error) { write (req->sock, ERROR, ERROR_LEN); goto abort; } break;
case 1: if (req->error) { write (req->sock, ERROR, ERROR_LEN); goto abort; } req->event = 2; ret = tux(TUX_ACTION_SEND_OBJECT, req); break;
case 2: abort: ret = tux(TUX_ACTION_FINISH_CLOSE_REQ, req); break; } return ret; }
|