source: trunk/minix/commands/httpd/reply.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.3 KB
Line 
1/* reply.c
2 *
3 * This file is part of httpd.
4 *
5 * 02/17/1996 Michael Temari <Michael@TemWare.Com>
6 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
7 * 12/29/2002 Michael Temari <Michael@TemWare.Com>
8 *
9 */
10#include <sys/types.h>
11#include <stdio.h>
12#include <string.h>
13#include <unistd.h>
14#include <errno.h>
15#include <signal.h>
16
17#include "http.h"
18#include "utility.h"
19#include "net.h"
20#include "config.h"
21
22#define SERVER "Server: "VERSION
23
24_PROTOTYPE(static void GotAlarm, (int sig));
25_PROTOTYPE(static int sendout, (char *data));
26
27static void GotAlarm(sig)
28int sig;
29{
30}
31
32static int sendout(data)
33char *data;
34{
35 if(strlen(data) > 0)
36 write(1, data, strlen(data));
37 write(1, "\r\n", 2);
38 if(dbglog != (FILE *)NULL) {
39 fprintf(dbglog, "REPLY: %s\n", data);
40 fflush(dbglog);
41 }
42
43 return(0);
44}
45
46int sendreply(rp, rq)
47struct http_reply *rp;
48struct http_request *rq;
49{
50int s;
51int s2;
52int e;
53static char buffer[8192];
54
55 if(rq->type != HTTP_REQUEST_TYPE_PROXY)
56 /* We're receiving data from a */
57 if(rq->method == HTTP_METHOD_POST ||
58 (rq->method == HTTP_METHOD_PUT && rp->status == HTTP_STATUS_OK)) {
59 if(rq->type != HTTP_REQUEST_TYPE_FULL)
60 return(0);
61 if(rq->method == HTTP_METHOD_PUT)
62 rp->status = HTTP_STATUS_CREATED;
63 else
64 rp->status = HTTP_STATUS_OK;
65 while(rq->size != 0) {
66 s = read(0, buffer, (rq->size > sizeof(buffer)) ? sizeof(buffer) : rq->size);
67 if(s <= 0) {
68 rp->status = HTTP_STATUS_SERVER_ERROR;
69 strcpy(rp->statusmsg, strerror(errno));
70 close(rp->fd);
71 close(rp->ofd);
72 break;
73 }
74 rq->size -= s;
75 s2 = write(rp->ofd, buffer, s);
76 if(s2 != s) break;
77 }
78 }
79
80 if(rp->status != HTTP_STATUS_OK && rp->status != HTTP_STATUS_CREATED &&
81 rp->status != HTTP_STATUS_NOT_MODIFIED)
82 rp->keepopen = 0;
83
84 if(rp->status == HTTP_STATUS_NOT_MODIFIED) {
85 sprintf(buffer, "<h2>Error %03d %s</h2>",
86 rp->status, rp->statusmsg);
87 rp->size = strlen(buffer);
88 rp->keepopen = rq->keepopen;
89 }
90
91 if(!rp->headers) {
92
93 if((rq->type == HTTP_REQUEST_TYPE_PROXY && rp->status != HTTP_STATUS_OK) ||
94 rq->type == HTTP_REQUEST_TYPE_FULL) {
95 sprintf(buffer, "HTTP/%d.%d %03d %s",
96 rq->vmajor, rq->vminor, rp->status, rp->statusmsg);
97 sendout(buffer);
98 sendout(SERVER);
99 if(rp->status == HTTP_STATUS_MOVED_PERM ||
100 rp->status == HTTP_STATUS_MOVED_TEMP) {
101#if 1
102 sprintf(buffer, "Location: %s", rq->url);
103#else
104 sprintf(buffer, "Location: http://%s%s", myhostname, rq->url);
105#endif
106 sendout(buffer);
107 }
108 if(rp->keepopen)
109 sendout("Connection: Keep-Alive");
110 else
111 sendout("Connection: Close");
112 if(rp->status == HTTP_STATUS_UNAUTHORIZED && rp->auth != NULL) {
113 sprintf(buffer, "WWW-Authenticate: Basic realm=\"%s\"", rp->auth->desc);
114 sendout(buffer);
115 }
116 if(rp->status == HTTP_STATUS_PROXY_AUTH_REQRD && proxyauth != NULL) {
117 sprintf(buffer, "Proxy-Authenticate: Basic realm=\"%s\"", proxyauth->desc);
118 sendout(buffer);
119 }
120 if(rp->modtime != (time_t) -1) {
121 sprintf(buffer, "Last-Modified: %s", httpdate(&rp->modtime));
122 sendout(buffer);
123 }
124 if(rp->size != 0) {
125 sprintf(buffer, "Content-Length: %lu", rp->size);
126 sendout(buffer);
127 }
128 if(rp->status == HTTP_STATUS_OK) {
129 sprintf(buffer, "Content-Type: %s", rp->mtype);
130 sendout(buffer);
131 } else
132 sendout("Content-Type: text/html");
133 if(!rp->headers)
134 sendout("");
135 } else
136 if(rp->status != HTTP_STATUS_OK)
137 return(0);
138 }
139
140 if(rp->status != HTTP_STATUS_OK && rp->status != HTTP_STATUS_CREATED) {
141 sprintf(buffer, "<h2>Error %03d %s</h2>",
142 rp->status, rp->statusmsg);
143 sendout(buffer);
144 return(0);
145 }
146
147 if(rq->type == HTTP_REQUEST_TYPE_PROXY) {
148 proxy(rq, rp);
149 return(0);
150 }
151
152 /* send out entity body */
153 if(rq->method == HTTP_METHOD_GET || rq->method == HTTP_METHOD_POST) {
154 errno = 0;
155 while(1) {
156 alarm(0);
157 signal(SIGALRM, GotAlarm);
158 alarm(10);
159 s = read(rp->fd, buffer, sizeof(buffer));
160 e = errno;
161 alarm(0);
162 if(s > 0) {
163 s2 = write(1, buffer, s);
164 e = errno;
165 if(s2 != s) break;
166 continue;
167 }
168 if(s == 0) break;
169 if(s < 0 && e != EINTR) break;
170 signal(SIGALRM, GotAlarm);
171 alarm(2);
172 s = read(0, buffer, 1);
173 e = errno;
174 alarm(0);
175 if(s < 0 && e != EINTR) break;
176 }
177 }
178
179 close(rp->fd);
180 rp->fd = -1;
181 if(rp->ofd != -1)
182 close(rp->ofd);
183 if(rp->pid != 0 && e != 0) {
184 kill(-rp->pid, SIGHUP);
185 rp->pid = 0;
186 }
187
188 return(0);
189}
Note: See TracBrowser for help on using the repository browser.