2 * "$Id: client.c,v 1.21 1999/06/17 19:40:25 mike Exp $"
4 * Client routines for the Common UNIX Printing System (CUPS) scheduler.
6 * Copyright 1997-1999 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44145 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * AcceptClient() - Accept a new client.
27 * CloseAllClients() - Close all remote clients immediately.
28 * CloseClient() - Close a remote client.
29 * ReadClient() - Read data from a client.
30 * SendCommand() - Send output from a command via HTTP.
31 * SendError() - Send an error message via HTTP.
32 * SendFile() - Send a file via HTTP.
33 * SendHeader() - Send an HTTP request.
34 * StartListening() - Create all listening sockets...
35 * StopListening() - Close all listening sockets...
36 * WriteClient() - Write data to a client as needed.
37 * check_if_modified() - Decode an "If-Modified-Since" line.
38 * decode_basic_auth() - Decode a Basic authorization string.
39 * get_file() - Get a filename and state info.
40 * pipe_command() - Pipe the output of a command to the remote client.
44 * Include necessary headers...
54 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
55 static void decode_basic_auth(client_t
*con
);
56 static char *get_file(client_t
*con
, struct stat
*filestats
);
57 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
61 * 'AcceptClient()' - Accept a new client.
65 AcceptClient(listener_t
*lis
) /* I - Listener socket */
67 int i
; /* Looping var */
68 int val
; /* Parameter value */
69 client_t
*con
; /* New client pointer */
70 unsigned address
;/* Address of client */
71 struct hostent
*host
; /* Host entry for address */
74 DEBUG_printf(("AcceptClient(%08x) %d NumClients = %d\n",
75 lis
, lis
->fd
, NumClients
));
78 * Get a pointer to the next available client...
81 con
= Clients
+ NumClients
;
83 memset(con
, 0, sizeof(client_t
));
84 con
->http
.activity
= time(NULL
);
87 * Accept the client and get the remote address...
90 val
= sizeof(struct sockaddr_in
);
92 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
95 LogMessage(LOG_ERROR
, "accept() failed - %s.", strerror(errno
));
99 con
->http
.hostaddr
.sin_port
= lis
->address
.sin_port
;
102 * Get the hostname or format the IP address as needed...
105 address
= ntohl(con
->http
.hostaddr
.sin_addr
.s_addr
);
109 host
= gethostbyaddr((char *)&address
, sizeof(address
), AF_INET
);
111 host
= gethostbyaddr(&address
, sizeof(address
), AF_INET
);
117 sprintf(con
->http
.hostname
, "%d.%d.%d.%d", (address
>> 24) & 255,
118 (address
>> 16) & 255, (address
>> 8) & 255, address
& 255);
120 strncpy(con
->http
.hostname
, host
->h_name
, sizeof(con
->http
.hostname
) - 1);
122 LogMessage(LOG_DEBUG
, "accept() %d from %s:%d.", con
->http
.fd
,
123 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.sin_port
));
126 * Add the socket to the select() input mask.
129 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
131 DEBUG_printf(("AcceptClient: Adding fd %d to InputSet...\n", con
->http
.fd
));
132 FD_SET(con
->http
.fd
, &InputSet
);
137 * Temporarily suspend accept()'s until we lose a client...
140 if (NumClients
== MAX_CLIENTS
)
141 for (i
= 0; i
< NumListeners
; i
++)
143 DEBUG_printf(("AcceptClient: Removing fd %d from InputSet...\n", Listeners
[i
].fd
));
144 FD_CLR(Listeners
[i
].fd
, &InputSet
);
150 * 'CloseAllClients()' - Close all remote clients immediately.
154 CloseAllClients(void)
156 while (NumClients
> 0)
157 CloseClient(Clients
);
162 * 'CloseClient()' - Close a remote client.
166 CloseClient(client_t
*con
) /* I - Client to close */
168 int i
; /* Looping var */
169 int status
; /* Exit status of pipe command */
172 LogMessage(LOG_DEBUG
, "CloseClient() %d", con
->http
.fd
);
175 * Close the socket and clear the file from the input set for select()...
178 #if defined(WIN32) || defined(__EMX__)
180 * Microsoft Windows and IBM OS/2 don't have a unified IO system...
183 closesocket(con
->http
.fd
);
190 #endif /* WIN32 || __EMX__ */
192 for (i
= 0; i
< NumListeners
; i
++)
194 DEBUG_printf(("CloseClient: Adding fd %d to InputSet...\n", Listeners
[i
].fd
));
195 FD_SET(Listeners
[i
].fd
, &InputSet
);
198 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->http
.fd
));
199 FD_CLR(con
->http
.fd
, &InputSet
);
200 if (con
->pipe_pid
!= 0)
202 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->file
));
203 FD_CLR(con
->file
, &InputSet
);
206 FD_CLR(con
->http
.fd
, &OutputSet
);
209 * If we have a data file open, close it...
216 kill(con
->pipe_pid
, SIGKILL
);
217 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
225 * Compact the list of clients as necessary...
230 if (con
< (Clients
+ NumClients
))
231 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
236 * 'ReadClient()' - Read data from a client.
239 int /* O - 1 on success, 0 on error */
240 ReadClient(client_t
*con
) /* I - Client to read from */
242 char line
[8192], /* Line from client... */
243 operation
[64], /* Operation code from socket */
244 version
[64]; /* HTTP version number string */
245 int major
, minor
; /* HTTP version numbers */
246 http_status_t status
; /* Transfer status */
247 ipp_state_t ipp_state
; /* State of IPP transfer */
248 int bytes
; /* Number of bytes to POST */
249 char *filename
; /* Name of file for GET/HEAD */
250 struct stat filestats
; /* File information */
251 mime_type_t
*type
; /* MIME type of file */
252 char command
[1024], /* Command to run */
253 *options
; /* Options/CGI data */
254 printer_t
*p
; /* Printer */
257 status
= HTTP_CONTINUE
;
259 switch (con
->http
.state
)
263 * See if we've received a request line...
266 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
273 * Ignore blank request lines...
280 * Clear other state variables...
283 httpClearFields(HTTP(con
));
285 con
->http
.activity
= time(NULL
);
286 con
->http
.version
= HTTP_1_0
;
287 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
288 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
289 con
->http
.data_remaining
= 0;
290 con
->operation
= HTTP_WAITING
;
294 con
->username
[0] = '\0';
295 con
->password
[0] = '\0';
298 if (con
->language
!= NULL
)
300 cupsLangFree(con
->language
);
301 con
->language
= NULL
;
305 * Grab the request line...
308 switch (sscanf(line
, "%63s%1023s%s", operation
, con
->uri
, version
))
311 SendError(con
, HTTP_BAD_REQUEST
);
315 con
->http
.version
= HTTP_0_9
;
318 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
320 SendError(con
, HTTP_BAD_REQUEST
);
327 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
328 if (con
->http
.version
== HTTP_1_1
)
329 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
331 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
335 SendError(con
, HTTP_NOT_SUPPORTED
);
343 * Process the request...
346 if (strcmp(operation
, "GET") == 0)
347 con
->http
.state
= HTTP_GET
;
348 else if (strcmp(operation
, "PUT") == 0)
349 con
->http
.state
= HTTP_PUT
;
350 else if (strcmp(operation
, "POST") == 0)
351 con
->http
.state
= HTTP_POST
;
352 else if (strcmp(operation
, "DELETE") == 0)
353 con
->http
.state
= HTTP_DELETE
;
354 else if (strcmp(operation
, "TRACE") == 0)
355 con
->http
.state
= HTTP_TRACE
;
356 else if (strcmp(operation
, "CLOSE") == 0)
357 con
->http
.state
= HTTP_CLOSE
;
358 else if (strcmp(operation
, "OPTIONS") == 0)
359 con
->http
.state
= HTTP_OPTIONS
;
360 else if (strcmp(operation
, "HEAD") == 0)
361 con
->http
.state
= HTTP_HEAD
;
364 SendError(con
, HTTP_BAD_REQUEST
);
369 con
->start
= time(NULL
);
370 con
->operation
= con
->http
.state
;
372 LogMessage(LOG_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
374 con
->http
.version
/ 100, con
->http
.version
% 100);
376 con
->http
.status
= HTTP_OK
;
387 * Parse incoming parameters until the status changes...
390 status
= httpUpdate(HTTP(con
));
392 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
394 SendError(con
, HTTP_BAD_REQUEST
);
402 * Handle new transfers...
405 if (status
== HTTP_OK
)
407 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
409 decode_basic_auth(con
);
411 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
412 con
->http
.version
>= HTTP_1_0
)
414 if (!SendError(con
, HTTP_BAD_REQUEST
))
420 else if (strncmp(con
->uri
, "..", 2) == 0)
423 * Protect against malicious users!
426 if (!SendError(con
, HTTP_FORBIDDEN
))
432 else if (con
->uri
[0] != '/')
435 * Don't allow proxying (yet)...
438 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
444 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
446 if (!SendError(con
, status
))
452 else switch (con
->http
.state
)
455 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
456 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
459 * Send PPD file - get the real printer name since printer
460 * names are not case sensitive but filename can be...
463 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
464 sprintf(con
->uri
, "/ppd/%s.ppd", p
->name
);
467 if (!SendError(con
, HTTP_NOT_FOUND
))
477 if (strncmp(con
->uri
, "/printers", 9) == 0 ||
478 strncmp(con
->uri
, "/classes", 8) == 0 ||
479 strncmp(con
->uri
, "/jobs", 5) == 0)
485 if (strncmp(con
->uri
, "/printers", 9) == 0)
487 sprintf(command
, "%s/cgi-bin/printers.cgi", ServerRoot
);
488 options
= con
->uri
+ 9;
490 else if (strncmp(con
->uri
, "/classes", 8) == 0)
492 sprintf(command
, "%s/cgi-bin/classes.cgi", ServerRoot
);
493 options
= con
->uri
+ 8;
497 sprintf(command
, "%s/cgi-bin/jobs.cgi", ServerRoot
);
498 options
= con
->uri
+ 5;
504 if (!SendCommand(con
, command
, options
))
506 if (!SendError(con
, HTTP_NOT_FOUND
))
513 LogRequest(con
, HTTP_OK
);
515 if (con
->http
.version
<= HTTP_1_0
)
516 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
524 if ((filename
= get_file(con
, &filestats
)) == NULL
)
526 if (!SendError(con
, HTTP_NOT_FOUND
))
532 else if (!check_if_modified(con
, &filestats
))
534 if (!SendError(con
, HTTP_NOT_MODIFIED
))
542 type
= mimeFileType(MimeDatabase
, filename
);
544 strcpy(line
, "text/plain");
546 sprintf(line
, "%s/%s", type
->super
, type
->type
);
548 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
557 case HTTP_POST_RECV
:
559 * See if the POST request includes a Content-Length field, and if
560 * so check the length against any limits that are set...
563 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
564 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
568 * Request too large...
571 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
581 * See what kind of POST request this is; for IPP requests the
582 * content-type field will be "application/ipp"...
585 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
586 con
->request
= ippNew();
587 else if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/x-www-form-urlencoded") == 0 &&
588 (strncmp(con
->uri
, "/printers", 9) == 0 ||
589 strncmp(con
->uri
, "/classes", 8) == 0 ||
590 strncmp(con
->uri
, "/jobs", 5) == 0))
596 if (strncmp(con
->uri
, "/printers", 9) == 0)
598 sprintf(command
, "%s/cgi-bin/printers", ServerRoot
);
599 options
= con
->uri
+ 9;
601 else if (strncmp(con
->uri
, "/classes", 8) == 0)
603 sprintf(command
, "%s/cgi-bin/classes", ServerRoot
);
604 options
= con
->uri
+ 8;
608 sprintf(command
, "%s/cgi-bin/jobs", ServerRoot
);
609 options
= con
->uri
+ 5;
615 if (!SendCommand(con
, command
, options
))
617 if (!SendError(con
, HTTP_NOT_FOUND
))
624 LogRequest(con
, HTTP_OK
);
626 if (con
->http
.version
<= HTTP_1_0
)
627 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
629 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
639 SendError(con
, HTTP_NOT_IMPLEMENTED
);
646 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
647 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
653 sprintf(command
, "/ppd/%s", con
->uri
+ 10);
654 strcpy(con
->uri
, command
);
657 if (strncmp(con
->uri
, "/printers/", 10) == 0 ||
658 strncmp(con
->uri
, "/classes/", 9) == 0 ||
659 strncmp(con
->uri
, "/jobs/", 6) == 0)
665 if (!SendHeader(con
, HTTP_OK
, "text/html"))
671 if (httpPrintf(HTTP(con
), "\r\n") < 0)
677 LogRequest(con
, HTTP_OK
);
679 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
681 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
687 LogRequest(con
, HTTP_NOT_FOUND
);
689 else if (!check_if_modified(con
, &filestats
))
691 if (!SendError(con
, HTTP_NOT_MODIFIED
))
697 LogRequest(con
, HTTP_NOT_MODIFIED
);
705 type
= mimeFileType(MimeDatabase
, filename
);
707 strcpy(line
, "text/plain");
709 sprintf(line
, "%s/%s", type
->super
, type
->type
);
711 if (!SendHeader(con
, HTTP_OK
, line
))
717 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
718 httpGetDateString(filestats
.st_mtime
)) < 0)
724 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
725 filestats
.st_size
) < 0)
731 LogRequest(con
, HTTP_OK
);
734 if (send(con
->http
.fd
, "\r\n", 2, 0) < 0)
740 con
->http
.state
= HTTP_WAITING
;
746 * Handle any incoming data...
749 switch (con
->http
.state
)
754 case HTTP_POST_RECV
:
755 LogMessage(LOG_DEBUG
, "ReadClient() %d con->data_encoding = %s con->data_remaining = %d",
757 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
758 con
->http
.data_remaining
);
759 DEBUG_printf(("ReadClient() %d con->data_encoding = %s con->data_remaining = %d\n",
761 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
762 con
->http
.data_remaining
));
764 if (con
->request
!= NULL
)
767 * Grab any request data from the connection...
770 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
772 LogMessage(LOG_ERROR
, "ReadClient() %d IPP Read Error!",
777 else if (ipp_state
!= IPP_DATA
)
780 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
783 * Create a file as needed for the request data...
786 sprintf(con
->filename
, "%s/requests/XXXXXX", ServerRoot
);
787 con
->file
= mkstemp(con
->filename
);
788 fchmod(con
->file
, 0644);
790 LogMessage(LOG_DEBUG
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
795 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
804 if (con
->http
.state
!= HTTP_POST_SEND
)
806 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
815 LogMessage(LOG_DEBUG
, "ReadClient() %d writing %d bytes", bytes
);
817 if (write(con
->file
, line
, bytes
) < bytes
)
821 unlink(con
->filename
);
823 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
830 else if (con
->http
.state
!= HTTP_POST_SEND
)
837 if (con
->http
.state
== HTTP_POST_SEND
)
841 fstat(con
->file
, &filestats
);
845 if (filestats
.st_size
> MaxRequestSize
&&
849 * Request is too big; remove it and send an error...
852 unlink(con
->filename
);
857 * Delete any IPP request data...
860 ippDelete(con
->request
);
864 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
873 ProcessIPPRequest(con
);
878 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
889 * 'SendCommand()' - Send output from a command via HTTP.
893 SendCommand(client_t
*con
,
897 con
->pipe_pid
= pipe_command(con
, 0, &(con
->file
), command
, options
);
899 LogMessage(LOG_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
900 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
902 if (con
->pipe_pid
== 0)
905 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
907 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
908 FD_SET(con
->file
, &InputSet
);
909 FD_SET(con
->http
.fd
, &OutputSet
);
911 if (!SendHeader(con
, HTTP_OK
, NULL
))
914 if (con
->http
.version
== HTTP_1_1
)
916 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
918 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
927 * 'SendError()' - Send an error message via HTTP.
930 int /* O - 1 if successful, 0 otherwise */
931 SendError(client_t
*con
, /* I - Connection */
932 http_status_t code
) /* I - Error code */
934 char message
[1024]; /* Text version of error code */
938 * Put the request in the access_log file...
941 LogRequest(con
, code
);
944 * To work around bugs in some proxies, don't use Keep-Alive for some
948 if (code
>= HTTP_BAD_REQUEST
)
949 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
952 * Send an error message back to the client. If the error code is a
953 * 400 or 500 series, make sure the message contains some text, too!
956 if (!SendHeader(con
, code
, NULL
))
959 if (code
== HTTP_UNAUTHORIZED
)
961 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
965 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
967 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
971 if (code
>= HTTP_BAD_REQUEST
)
974 * Send a human-readable error message.
977 sprintf(message
, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
978 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
979 code
, httpStatus(code
), httpStatus(code
),
980 con
->language
? con
->language
->messages
[code
] : httpStatus(code
));
982 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
984 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
986 if (httpPrintf(HTTP(con
), "\r\n") < 0)
988 if (send(con
->http
.fd
, message
, strlen(message
), 0) < 0)
991 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
994 con
->http
.state
= HTTP_WAITING
;
1001 * 'SendFile()' - Send a file via HTTP.
1005 SendFile(client_t
*con
,
1009 struct stat
*filestats
)
1011 con
->file
= open(filename
, O_RDONLY
);
1013 LogMessage(LOG_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1018 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1022 if (!SendHeader(con
, code
, type
))
1025 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1027 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1029 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1032 FD_SET(con
->http
.fd
, &OutputSet
);
1039 * 'SendHeader()' - Send an HTTP request.
1042 int /* O - 1 on success, 0 on failure */
1043 SendHeader(client_t
*con
, /* I - Client to send to */
1044 http_status_t code
, /* I - HTTP status code */
1045 char *type
) /* I - MIME type of document */
1047 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1048 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1050 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1052 if (httpPrintf(HTTP(con
), "Server: CUPS/1.0\r\n") < 0)
1054 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1056 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1058 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1061 if (con
->language
!= NULL
)
1063 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1064 con
->language
->language
) < 0)
1068 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1069 cupsLangEncoding(con
->language
)) < 0)
1072 else if (type
!= NULL
)
1073 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1081 * 'StartListening()' - Create all listening sockets...
1085 StartListening(void)
1087 int i
, /* Looping var */
1088 val
; /* Parameter value */
1089 listener_t
*lis
; /* Current listening socket */
1092 #if defined(WIN32) || defined(__EMX__)
1094 * Initialize Windows sockets... This handles loading the DLL, etc. since
1095 * Windows doesn't have built-in support for sockets like UNIX...
1098 WSADATA wsadata
; /* Socket data for application from DLL */
1099 static int initialized
= 0;/* Whether or not we've initialized things */
1106 if (WSAStartup(MAKEWORD(1,1), &wsadata
) != 0)
1112 * Setup socket listeners...
1115 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
1117 LogMessage(LOG_DEBUG
, "StartListening() address=%08x port=%d",
1118 ntohl(lis
->address
.sin_addr
.s_addr
),
1119 ntohs(lis
->address
.sin_port
));
1121 if ((lis
->fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
1123 LogMessage(LOG_ERROR
, "StartListening() Unable to open listen socket - %s.",
1128 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
1131 * Set things up to reuse the local address for this port.
1136 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1138 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
1142 * Bind to the port we found...
1145 if (bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
), sizeof(lis
->address
)) < 0)
1147 LogMessage(LOG_ERROR
, "StartListening() Unable to bind socket - %s.", strerror(errno
));
1152 * Listen for new clients.
1155 if (listen(lis
->fd
, SOMAXCONN
) < 0)
1157 LogMessage(LOG_ERROR
, "StartListening() Unable to listen for clients - %s.",
1163 * Setup the select() input mask to contain the listening socket we have.
1166 DEBUG_printf(("StartListening: Adding fd %d to InputSet...\n", lis
->fd
));
1167 FD_SET(lis
->fd
, &InputSet
);
1170 LogMessage(LOG_DEBUG
, "StartListening() NumListeners=%d", NumListeners
);
1175 * 'StopListening()' - Close all listening sockets...
1181 int i
; /* Looping var */
1182 listener_t
*lis
; /* Current listening socket */
1185 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
1187 #if defined(WIN32) || defined(__EMX__)
1188 closesocket(lis
->fd
);
1191 #endif /* WIN32 || __EMX__ */
1193 DEBUG_printf(("StopListening: Removing fd %d from InputSet...\n", lis
->fd
));
1194 FD_CLR(lis
->fd
, &InputSet
);
1197 LogMessage(LOG_DEBUG
, "StopListening()");
1202 * 'WriteClient()' - Write data to a client as needed.
1205 int /* O - 1 if success, 0 if fail */
1206 WriteClient(client_t
*con
) /* I - Client connection */
1208 int bytes
; /* Number of bytes written */
1209 char buf
[HTTP_MAX_BUFFER
]; /* Data buffer */
1210 ipp_state_t ipp_state
; /* IPP state value */
1213 if (con
->http
.state
!= HTTP_GET_SEND
&&
1214 con
->http
.state
!= HTTP_POST_SEND
)
1217 if (con
->response
!= NULL
)
1219 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1220 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1222 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
))) > 0)
1224 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1230 con
->bytes
+= bytes
;
1235 LogRequest(con
, HTTP_OK
);
1237 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1239 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1246 con
->http
.state
= HTTP_WAITING
;
1248 FD_CLR(con
->http
.fd
, &OutputSet
);
1252 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1253 FD_CLR(con
->file
, &InputSet
);
1256 kill(con
->pipe_pid
, SIGTERM
);
1263 if (con
->request
!= NULL
)
1265 ippDelete(con
->request
);
1266 con
->request
= NULL
;
1269 if (con
->response
!= NULL
)
1271 ippDelete(con
->response
);
1272 con
->response
= NULL
;
1275 if (!con
->http
.keep_alive
)
1282 LogMessage(LOG_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1284 con
->http
.activity
= time(NULL
);
1291 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1294 static int /* O - 1 if modified since */
1295 check_if_modified(client_t
*con
, /* I - Client connection */
1296 struct stat
*filestats
) /* I - File information */
1298 char *ptr
; /* Pointer into field */
1299 time_t date
; /* Time/date value */
1300 int size
; /* Size/length value */
1305 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1310 LogMessage(LOG_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1313 while (*ptr
!= '\0')
1315 while (isspace(*ptr
) || *ptr
== ';')
1318 if (strncasecmp(ptr
, "length=", 7) == 0)
1323 while (isdigit(*ptr
))
1326 else if (isalpha(*ptr
))
1328 date
= httpGetDateTime(ptr
);
1329 while (*ptr
!= '\0' && *ptr
!= ';')
1334 LogMessage(LOG_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1335 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1337 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1338 (date
< filestats
->st_mtime
&& date
!= 0) ||
1339 (size
== 0 && date
== 0));
1344 * 'decode_basic_auth()' - Decode a Basic authorization string.
1348 decode_basic_auth(client_t
*con
) /* I - Client to decode to */
1350 char *s
, /* Authorization string */
1351 value
[1024]; /* Value string */
1355 * Decode the string and pull the username and password out...
1358 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1359 if (strncmp(s
, "Basic", 5) != 0)
1366 httpDecode64(value
, s
);
1368 LogMessage(LOG_DEBUG
, "decode_basic_auth() %d Authorization=\"%s\"",
1369 con
->http
.fd
, value
);
1371 sscanf(value
, "%[^:]:%[^\n]", con
->username
, con
->password
);
1376 * 'get_file()' - Get a filename and state info.
1379 static char * /* O - Real filename */
1380 get_file(client_t
*con
, /* I - Client connection */
1381 struct stat
*filestats
)/* O - File information */
1383 int status
; /* Status of filesystem calls */
1384 char *params
; /* Pointer to parameters in URI */
1385 static char filename
[1024]; /* Filename buffer */
1389 * Need to add DocumentRoot global...
1392 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1393 sprintf(filename
, "%s%s", ServerRoot
, con
->uri
);
1394 else if (con
->language
!= NULL
)
1395 sprintf(filename
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
1398 sprintf(filename
, "%s%s", DocumentRoot
, con
->uri
);
1400 if ((params
= strchr(filename
, '?')) != NULL
)
1404 * Grab the status for this language; if there isn't a language-specific file
1405 * then fallback to the default one...
1408 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1411 * Drop the language prefix and try the current directory...
1414 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1416 sprintf(filename
, "%s%s", DocumentRoot
, con
->uri
);
1418 status
= stat(filename
, filestats
);
1423 * If we're found a directory, get the index.html file instead...
1426 if (!status
&& S_ISDIR(filestats
->st_mode
))
1428 if (filename
[strlen(filename
) - 1] == '/')
1429 strcat(filename
, "index.html");
1431 strcat(filename
, "/index.html");
1433 status
= stat(filename
, filestats
);
1436 LogMessage(LOG_DEBUG
, "get_filename() %d filename=%s size=%d",
1437 con
->http
.fd
, filename
, filestats
->st_size
);
1447 * 'pipe_command()' - Pipe the output of a command to the remote client.
1450 static int /* O - Process ID */
1451 pipe_command(client_t
*con
, /* I - Client connection */
1452 int infile
, /* I - Standard input for command */
1453 int *outfile
, /* O - Standard output for command */
1454 char *command
, /* I - Command to run */
1455 char *options
) /* I - Options for command */
1457 int pid
; /* Process ID */
1458 char *commptr
; /* Command string pointer */
1459 int fds
[2]; /* Pipe FDs */
1460 int argc
; /* Number of arguments */
1461 char argbuf
[1024], /* Argument buffer */
1462 *argv
[100], /* Argument strings */
1463 *envp
[100]; /* Environment variables */
1464 char hostname
[1024]; /* Hostname string */
1465 static char lang
[1024]; /* LANG env variable */
1466 static char content_length
[1024]; /* CONTENT_LENGTH env variable */
1467 static char content_type
[1024]; /* CONTENT_TYPE env variable */
1468 static char server_port
[1024]; /* Default listen port */
1469 static char server_name
[1024]; /* Default listen hostname */
1470 static char remote_host
[1024]; /* REMOTE_HOST env variable */
1471 static char remote_user
[1024]; /* REMOTE_HOST env variable */
1475 * Copy the command string...
1478 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1479 argbuf
[sizeof(argbuf
) - 1] = '\0';
1482 * Parse the string; arguments can be separated by spaces or by ? or +...
1487 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1488 if (*commptr
== ' ' || *commptr
== '?' || *commptr
== '+')
1492 while (*commptr
== ' ')
1495 if (*commptr
!= '\0')
1497 argv
[argc
] = commptr
;
1503 else if (*commptr
== '%')
1505 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1506 *commptr
= (commptr
[1] - '0') << 4;
1508 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1510 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1511 *commptr
|= commptr
[2] - '0';
1513 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1515 strcpy(commptr
+ 1, commptr
+ 3);
1520 if (argv
[0][0] == '\0')
1521 argv
[0] = strrchr(command
, '/') + 1;
1524 * Setup the environment variables as needed...
1527 gethostname(hostname
, sizeof(hostname
) - 1);
1529 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1530 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1531 sprintf(server_name
, "SERVER_NAME=%s", hostname
);
1532 sprintf(remote_host
, "REMOTE_HOST=%s", con
->http
.hostname
);
1533 sprintf(remote_user
, "REMOTE_USER=%s", con
->username
);
1535 envp
[0] = "PATH=/bin:/usr/bin";
1536 envp
[1] = "SERVER_SOFTWARE=CUPS/1.0";
1537 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1538 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1539 envp
[4] = server_name
;
1540 envp
[5] = server_port
;
1541 envp
[6] = remote_host
;
1542 envp
[7] = remote_user
;
1546 if (con
->operation
== HTTP_GET
)
1548 envp
[10] = "REQUEST_METHOD=GET";
1553 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->http
.data_remaining
);
1554 sprintf(content_type
, "CONTENT_TYPE=%s",
1555 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1557 envp
[10] = "REQUEST_METHOD=POST";
1558 envp
[11] = content_length
;
1559 envp
[12] = content_type
;
1564 * Create a pipe for the output...
1571 * Then execute the pipe command...
1574 if ((pid
= fork()) == 0)
1577 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1596 * Execute the pipe program; if an error occurs, exit with status 1...
1599 execve(command
, argv
, envp
);
1600 perror("execve failed");
1607 * Error - can't fork!
1617 * Fork successful - return the PID...
1629 * End of "$Id: client.c,v 1.21 1999/06/17 19:40:25 mike Exp $".