1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "httpd.h"
/* ---------------------------------------------------------------------- */
static struct HTTP_STATUS {
int status;
char *head;
char *body;
} http[] = {
{ 200, "200 OK", NULL },
{ 400, "400 Bad Request", "*PLONK*\n" },
{ 404, "404 Not Found", "videotext page not found in cache\n" },
{ 408, "408 Request Timeout", "Request Timeout\n" },
{ 500, "500 Internal Server Error", "Sorry folks\n" },
{ 501, "501 Not Implemented", "Sorry folks\n" },
{ 0, NULL, NULL }
};
/* ---------------------------------------------------------------------- */
#define RESPONSE_START \
"HTTP/1.1 %s\r\n" \
"Server: %s\r\n" \
"Connection: %s\r\n"
#define RFCTIME \
"%a, %d %b %Y %H:%M:%S GMT"
#define BOUNDARY \
"XXX_CUT_HERE_%ld_XXX"
void
mkerror(struct REQUEST *req, int status, int ka)
{
int i;
for (i = 0; http[i].status != 0; i++)
if (http[i].status == status)
break;
req->status = status;
req->body = http[i].body;
req->lbody = strlen(req->body);
if (!ka)
req->keep_alive = 0;
req->lres = sprintf(req->hres,
RESPONSE_START
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n",
http[i].head,server_name,
req->keep_alive ? "Keep-Alive" : "Close",
req->lbody);
if (401 == status)
req->lres += sprintf(req->hres+req->lres,
"WWW-Authenticate: Basic, realm=\"webfs\"\r\n");
req->lres += strftime(req->hres+req->lres,80,
"Date: " RFCTIME "\r\n\r\n",
gmtime(&now));
req->state = STATE_WRITE_HEADER;
if (debug)
fprintf(stderr,"%03d: error: %d, connection=%s\n",
req->fd, status, req->keep_alive ? "Keep-Alive" : "Close");
}
void
mkredirect(struct REQUEST *req)
{
req->status = 302;
req->body = req->path;
req->lbody = strlen(req->body);
req->lres = sprintf(req->hres,
RESPONSE_START
"Location: http://%s:%d%s\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n",
"302 Redirect",server_name,
req->keep_alive ? "Keep-Alive" : "Close",
req->hostname,tcp_port,req->path,
req->lbody);
req->lres += strftime(req->hres+req->lres,80,
"Date: " RFCTIME "\r\n\r\n",
gmtime(&now));
req->state = STATE_WRITE_HEADER;
if (debug)
fprintf(stderr,"%03d: 302 redirect: %s, connection=%s\n",
req->fd, req->path, req->keep_alive ? "Keep-Alive" : "Close");
}
void
mkheader(struct REQUEST *req, int status, time_t mtime)
{
int i;
for (i = 0; http[i].status != 0; i++)
if (http[i].status == status)
break;
req->status = status;
req->lres = sprintf(req->hres,
RESPONSE_START,
http[i].head,server_name,
req->keep_alive ? "Keep-Alive" : "Close");
req->lres += sprintf(req->hres+req->lres,
"Content-Type: %s\r\n"
"Content-Length: %d\r\n",
req->mime,
req->lbody);
if (mtime != -1)
req->lres += strftime(req->hres+req->lres,80,
"Last-Modified: " RFCTIME "\r\n",
gmtime(&mtime));
req->lres += strftime(req->hres+req->lres,80,
"Date: " RFCTIME "\r\n\r\n",
gmtime(&now));
req->state = STATE_WRITE_HEADER;
if (debug)
fprintf(stderr,"%03d: %d, connection=%s\n",
req->fd, status, req->keep_alive ? "Keep-Alive" : "Close");
}
/* ---------------------------------------------------------------------- */
void write_request(struct REQUEST *req)
{
int rc;
for (;;) {
switch (req->state) {
case STATE_WRITE_HEADER:
rc = write(req->fd,req->hres + req->written,
req->lres - req->written);
switch (rc) {
case -1:
if (errno == EAGAIN)
return;
if (errno == EINTR)
continue;
xperror(LOG_INFO,"write",req->peerhost);
/* fall through */
case 0:
req->state = STATE_CLOSE;
return;
default:
req->written += rc;
req->bc += rc;
if (req->written != req->lres)
return;
}
req->written = 0;
if (req->head_only) {
req->state = STATE_FINISHED;
return;
} else {
req->state = STATE_WRITE_BODY;
}
break;
case STATE_WRITE_BODY:
rc = write(req->fd,req->body + req->written,
req->lbody - req->written);
switch (rc) {
case -1:
if (errno == EAGAIN)
return;
if (errno == EINTR)
continue;
xperror(LOG_INFO,"write",req->peerhost);
/* fall through */
case 0:
req->state = STATE_CLOSE;
return;
default:
req->written += rc;
req->bc += rc;
if (req->written != req->lbody)
return;
}
req->state = STATE_FINISHED;
return;
} /* switch(state) */
} /* for (;;) */
}
|