3 * This file is part of httpd.
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>
10 #include <sys/types.h>
22 #define SERVER "Server: "VERSION
24 _PROTOTYPE(static void GotAlarm
, (int sig
));
25 _PROTOTYPE(static int sendout
, (char *data
));
27 static void GotAlarm(sig
)
32 static int sendout(data
)
36 write(1, data
, strlen(data
));
38 if(dbglog
!= (FILE *)NULL
) {
39 fprintf(dbglog
, "REPLY: %s\n", data
);
47 struct http_reply
*rp
;
48 struct http_request
*rq
;
53 static char buffer
[8192];
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
)
61 if(rq
->method
== HTTP_METHOD_PUT
)
62 rp
->status
= HTTP_STATUS_CREATED
;
64 rp
->status
= HTTP_STATUS_OK
;
65 while(rq
->size
!= 0) {
66 s
= read(0, buffer
, (rq
->size
> sizeof(buffer
)) ? sizeof(buffer
) : rq
->size
);
68 rp
->status
= HTTP_STATUS_SERVER_ERROR
;
69 strcpy(rp
->statusmsg
, strerror(errno
));
75 s2
= write(rp
->ofd
, buffer
, s
);
80 if(rp
->status
!= HTTP_STATUS_OK
&& rp
->status
!= HTTP_STATUS_CREATED
&&
81 rp
->status
!= HTTP_STATUS_NOT_MODIFIED
)
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
;
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
);
99 if(rp
->status
== HTTP_STATUS_MOVED_PERM
||
100 rp
->status
== HTTP_STATUS_MOVED_TEMP
) {
102 sprintf(buffer
, "Location: %s", rq
->url
);
104 sprintf(buffer
, "Location: http://%s%s", myhostname
, rq
->url
);
109 sendout("Connection: Keep-Alive");
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
);
116 if(rp
->status
== HTTP_STATUS_PROXY_AUTH_REQRD
&& proxyauth
!= NULL
) {
117 sprintf(buffer
, "Proxy-Authenticate: Basic realm=\"%s\"", proxyauth
->desc
);
120 if(rp
->modtime
!= (time_t) -1) {
121 sprintf(buffer
, "Last-Modified: %s", httpdate(&rp
->modtime
));
125 sprintf(buffer
, "Content-Length: %lu", rp
->size
);
128 if(rp
->status
== HTTP_STATUS_OK
) {
129 sprintf(buffer
, "Content-Type: %s", rp
->mtype
);
132 sendout("Content-Type: text/html");
136 if(rp
->status
!= HTTP_STATUS_OK
)
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
);
147 if(rq
->type
== HTTP_REQUEST_TYPE_PROXY
) {
152 /* send out entity body */
153 if(rq
->method
== HTTP_METHOD_GET
|| rq
->method
== HTTP_METHOD_POST
) {
157 signal(SIGALRM
, GotAlarm
);
159 s
= read(rp
->fd
, buffer
, sizeof(buffer
));
163 s2
= write(1, buffer
, s
);
169 if(s
< 0 && e
!= EINTR
) break;
170 signal(SIGALRM
, GotAlarm
);
172 s
= read(0, buffer
, 1);
175 if(s
< 0 && e
!= EINTR
) break;
183 if(rp
->pid
!= 0 && e
!= 0) {
184 kill(-rp
->pid
, SIGHUP
);