2 * "$Id: client.c,v 1.4 1999/02/19 22:07:04 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 header.
34 * StartListening() - Create all listening sockets...
35 * StopListening() - Close all listening sockets...
36 * WriteClient() - Write data to a client as needed.
37 * decode_basic_auth() - Decode a Basic authorization string.
38 * get_datetime() - Get a data/time string for the given time.
39 * get_extension() - Get the extension for a filename.
40 * get_file() - Get a filename and state info.
41 * get_line() - Get a request line terminated with a CR and LF.
42 * get_message() - Get a message string for the given HTTP code.
43 * get_type() - Get MIME type from the given extension.
44 * sigpipe_handler() - Handle 'broken pipe' signals from lost network
49 * Include necessary headers...
59 static void decode_basic_auth(client_t
*con
);
60 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
61 static char *get_extension(char *filename
);
62 static char *get_type(char *extension
);
63 static char *get_file(client_t
*con
, struct stat
*filestats
);
64 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
65 static void sigpipe_handler(int sig
);
69 * 'AcceptClient()' - Accept a new client.
73 AcceptClient(listener_t
*lis
) /* I - Listener socket */
75 int i
; /* Looping var */
76 int val
; /* Parameter value */
77 client_t
*con
; /* New client pointer */
78 unsigned address
;/* Address of client */
79 struct hostent
*host
; /* Host entry for address */
83 * Get a pointer to the next available client...
86 con
= Clients
+ NumClients
;
88 memset(con
, 0, sizeof(client_t
));
89 con
->http
.activity
= time(NULL
);
92 * Accept the client and get the remote address...
95 val
= sizeof(struct sockaddr_in
);
97 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
), &val
)) < 0)
99 LogMessage(LOG_ERROR
, "accept() failed - %s.", strerror(errno
));
104 * Get the hostname or format the IP address as needed...
107 address
= ntohl(con
->http
.hostaddr
.sin_addr
.s_addr
);
110 host
= gethostbyaddr(&address
, sizeof(address
), AF_INET
);
115 sprintf(con
->http
.hostname
, "%d.%d.%d.%d", (address
>> 24) & 255,
116 (address
>> 16) & 255, (address
>> 8) & 255, address
& 255);
118 strncpy(con
->http
.hostname
, host
->h_name
, sizeof(con
->http
.hostname
) - 1);
120 LogMessage(LOG_INFO
, "accept() %d from %s.", con
->http
.fd
, con
->http
.hostname
);
123 * Add the socket to the select() input mask.
126 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
128 FD_SET(con
->http
.fd
, &InputSet
);
133 * Temporarily suspend accept()'s until we lose a client...
136 if (NumClients
== MAX_CLIENTS
)
137 for (i
= 0; i
< NumListeners
; i
++)
138 FD_CLR(Listeners
[i
].fd
, &InputSet
);
143 * 'CloseAllClients()' - Close all remote clients immediately.
147 CloseAllClients(void)
149 while (NumClients
> 0)
150 CloseClient(Clients
);
155 * 'CloseClient()' - Close a remote client.
159 CloseClient(client_t
*con
) /* I - Client to close */
161 int i
; /* Looping var */
162 int status
; /* Exit status of pipe command */
165 LogMessage(LOG_INFO
, "CloseClient() %d", con
->http
.fd
);
168 * Close the socket and clear the file from the input set for select()...
171 #if defined(WIN32) || defined(__EMX__)
173 * Microsoft Windows and IBM OS/2 don't have a unified IO system...
176 closesocket(con
->http
.fd
);
183 #endif /* WIN32 || __EMX__ */
185 for (i
= 0; i
< NumListeners
; i
++)
186 FD_SET(Listeners
[i
].fd
, &InputSet
);
188 FD_CLR(con
->http
.fd
, &InputSet
);
189 if (con
->pipe_pid
!= 0)
190 FD_CLR(con
->file
, &InputSet
);
191 FD_CLR(con
->http
.fd
, &OutputSet
);
194 * If we have a data file open, close it...
201 kill(con
->pipe_pid
, SIGKILL
);
202 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
209 * Compact the list of clients as necessary...
214 if (con
< (Clients
+ NumClients
))
215 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
220 * 'ReadClient()' - Read data from a client.
223 int /* O - 1 on success, 0 on error */
224 ReadClient(client_t
*con
) /* I - Client to read from */
226 char line
[8192], /* Line from client... */
227 operation
[64], /* Operation code from socket */
228 version
[64]; /* HTTP version number string */
229 int major
, minor
; /* HTTP version numbers */
230 http_status_t status
; /* Transfer status */
231 int bytes
; /* Number of bytes to POST */
232 char *filename
, /* Name of file for GET/HEAD */
233 *extension
, /* Extension of file */
234 *type
; /* MIME type */
235 struct stat filestats
; /* File information */
236 char command
[1024], /* Command to run */
237 *options
; /* Options/CGI data */
240 status
= HTTP_CONTINUE
;
242 switch (con
->http
.state
)
246 * See if we've received a request line...
249 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
253 * Ignore blank request lines...
260 * Clear other state variables...
263 httpClearFields(HTTP(con
));
265 con
->http
.activity
= time(NULL
);
266 con
->http
.version
= HTTP_1_0
;
267 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
268 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
269 con
->http
.data_remaining
= 0;
270 con
->operation
= HTTP_WAITING
;
274 con
->username
[0] = '\0';
275 con
->password
[0] = '\0';
278 if (con
->language
!= NULL
)
280 cupsLangFree(con
->language
);
281 con
->language
= NULL
;
285 * Grab the request line...
288 switch (sscanf(line
, "%63s%1023s%s", operation
, con
->uri
, version
))
291 SendError(con
, HTTP_BAD_REQUEST
);
295 con
->http
.version
= HTTP_0_9
;
298 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
300 SendError(con
, HTTP_BAD_REQUEST
);
307 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
308 if (con
->http
.version
== HTTP_1_1
)
309 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
311 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
315 SendError(con
, HTTP_NOT_SUPPORTED
);
323 * Process the request...
326 if (strcmp(operation
, "GET") == 0)
327 con
->http
.state
= HTTP_GET
;
328 else if (strcmp(operation
, "PUT") == 0)
329 con
->http
.state
= HTTP_PUT
;
330 else if (strcmp(operation
, "POST") == 0)
331 con
->http
.state
= HTTP_POST
;
332 else if (strcmp(operation
, "DELETE") == 0)
333 con
->http
.state
= HTTP_DELETE
;
334 else if (strcmp(operation
, "TRACE") == 0)
335 con
->http
.state
= HTTP_TRACE
;
336 else if (strcmp(operation
, "CLOSE") == 0)
337 con
->http
.state
= HTTP_CLOSE
;
338 else if (strcmp(operation
, "OPTIONS") == 0)
339 con
->http
.state
= HTTP_OPTIONS
;
340 else if (strcmp(operation
, "HEAD") == 0)
341 con
->http
.state
= HTTP_HEAD
;
344 SendError(con
, HTTP_BAD_REQUEST
);
349 con
->start
= time(NULL
);
350 con
->operation
= con
->http
.state
;
352 LogMessage(LOG_INFO
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
354 con
->http
.version
/ 100, con
->http
.version
% 100);
356 con
->http
.status
= HTTP_OK
;
367 * Parse incoming parameters until the status changes...
370 status
= httpUpdate(HTTP(con
));
372 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
374 SendError(con
, HTTP_BAD_REQUEST
);
382 * Handle new transfers...
385 if (status
== HTTP_OK
)
387 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
389 decode_basic_auth(con
);
391 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
392 con
->http
.version
>= HTTP_1_0
)
394 if (!SendError(con
, HTTP_BAD_REQUEST
))
400 else if (strncmp(con
->uri
, "..", 2) == 0)
403 * Protect against malicious users!
406 if (!SendError(con
, HTTP_FORBIDDEN
))
412 else if (con
->uri
[0] != '/')
415 * Don't allow proxying (yet)...
418 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
424 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
426 if (!SendError(con
, status
))
432 else switch (con
->http
.state
)
435 if (strncmp(con
->uri
, "/printers", 9) == 0 ||
436 strncmp(con
->uri
, "/classes", 8) == 0 ||
437 strncmp(con
->uri
, "/jobs", 5) == 0)
443 if (strncmp(con
->uri
, "/printers", 9) == 0)
445 sprintf(command
, "%s/cgi-bin/printers", ServerRoot
);
446 options
= con
->uri
+ 9;
448 else if (strncmp(con
->uri
, "/classes", 8) == 0)
450 sprintf(command
, "%s/cgi-bin/classes", ServerRoot
);
451 options
= con
->uri
+ 8;
455 sprintf(command
, "%s/cgi-bin/jobs", ServerRoot
);
456 options
= con
->uri
+ 5;
462 if (!SendCommand(con
, command
, options
))
464 if (!SendError(con
, HTTP_NOT_FOUND
))
471 LogRequest(con
, HTTP_OK
);
473 if (con
->http
.version
<= HTTP_1_0
)
474 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
482 if ((filename
= get_file(con
, &filestats
)) == NULL
)
484 if (!SendError(con
, HTTP_NOT_FOUND
))
490 else if (!check_if_modified(con
, &filestats
))
492 if (!SendError(con
, HTTP_NOT_MODIFIED
))
500 extension
= get_extension(filename
);
501 type
= get_type(extension
);
503 if (!SendFile(con
, HTTP_OK
, filename
, type
, &filestats
))
512 case HTTP_POST_RECV
:
514 * See what kind of POST request this is; for IPP requests the
515 * content-type field will be "application/ipp"...
518 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
521 sprintf(con
->filename
, "%s/requests/XXXXXX", ServerRoot
);
522 con
->file
= mkstemp(con
->filename
);
524 LogMessage(LOG_INFO
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
529 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
536 con
->request
= ippNew();
539 else if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0 &&
540 (strncmp(con
->uri
, "/printers", 9) == 0 ||
541 strncmp(con
->uri
, "/classes", 8) == 0 ||
542 strncmp(con
->uri
, "/jobs", 5) == 0))
548 if (strncmp(con
->uri
, "/printers", 9) == 0)
550 sprintf(command
, "%s/cgi-bin/printers", ServerRoot
);
551 options
= con
->uri
+ 9;
553 else if (strncmp(con
->uri
, "/classes", 8) == 0)
555 sprintf(command
, "%s/cgi-bin/classes", ServerRoot
);
556 options
= con
->uri
+ 8;
560 sprintf(command
, "%s/cgi-bin/jobs", ServerRoot
);
561 options
= con
->uri
+ 5;
567 if (!SendCommand(con
, command
, options
))
569 if (!SendError(con
, HTTP_NOT_FOUND
))
576 LogRequest(con
, HTTP_OK
);
578 if (con
->http
.version
<= HTTP_1_0
)
579 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
581 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
591 SendError(con
, HTTP_NOT_IMPLEMENTED
);
598 if (strncmp(con
->uri
, "/printers/", 10) == 0 ||
599 strncmp(con
->uri
, "/classes/", 9) == 0 ||
600 strncmp(con
->uri
, "/jobs/", 6) == 0)
606 if (!SendHeader(con
, HTTP_OK
, "text/html"))
612 if (httpPrintf(HTTP(con
), "\r\n") < 0)
618 LogRequest(con
, HTTP_OK
);
620 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
622 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
628 LogRequest(con
, HTTP_NOT_FOUND
);
630 else if (!check_if_modified(con
, &filestats
))
632 if (!SendError(con
, HTTP_NOT_MODIFIED
))
638 LogRequest(con
, HTTP_NOT_MODIFIED
);
646 extension
= get_extension(filename
);
647 type
= get_type(extension
);
649 if (!SendHeader(con
, HTTP_OK
, type
))
655 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
656 httpGetDateString(filestats
.st_mtime
)) < 0)
662 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
663 filestats
.st_size
) < 0)
669 LogRequest(con
, HTTP_OK
);
672 if (send(con
->http
.fd
, "\r\n", 2, 0) < 0)
678 con
->http
.state
= HTTP_WAITING
;
684 * Handle any incoming data...
687 switch (con
->http
.state
)
692 case HTTP_POST_RECV
:
693 LogMessage(LOG_DEBUG
, "ReadClient() %d con->data_encoding = %s con->data_remaining = %d\n",
694 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
695 con
->http
.data_remaining
);
697 if (con
->request
!= NULL
)
700 * Grab any request data from the connection...
703 if (ippRead(&(con
->http
), con
->request
) != IPP_DATA
)
707 * Then create a file as needed for the request data...
710 if (con
->file
== 0 &&
711 (con
->http
.data_remaining
> 0 ||
712 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
))
714 sprintf(con
->filename
, "%s/requests/XXXXXX", ServerRoot
);
715 con
->file
= mkstemp(con
->filename
);
717 LogMessage(LOG_INFO
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
722 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
731 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
736 else if (con
->http
.state
== HTTP_POST_SEND
)
743 * Add IPP processing/response stuff...
746 if (!SendError(con
, HTTP_ACCEPTED
))
757 LogMessage(LOG_DEBUG
, "ReadClient() %d writing %d bytes", bytes
);
759 if (write(con
->file
, line
, bytes
) < bytes
)
762 unlink(con
->filename
);
764 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
774 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
785 * 'SendCommand()' - Send output from a command via HTTP.
789 SendCommand(client_t
*con
,
793 con
->pipe_pid
= pipe_command(con
, 0, &(con
->file
), command
, options
);
795 LogMessage(LOG_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
796 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
798 if (con
->pipe_pid
== 0)
801 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
803 FD_SET(con
->file
, &InputSet
);
804 FD_SET(con
->http
.fd
, &OutputSet
);
806 if (!SendHeader(con
, HTTP_OK
, "text/html"))
809 if (con
->http
.version
== HTTP_1_1
)
811 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
813 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
817 if (httpPrintf(HTTP(con
), "\r\n") < 0)
825 * 'SendError()' - Send an error message via HTTP.
828 int /* O - 1 if successful, 0 otherwise */
829 SendError(client_t
*con
, /* I - Connection */
830 http_status_t code
) /* I - Error code */
832 char message
[1024]; /* Text version of error code */
836 * Put the request in the access_log file...
839 LogRequest(con
, code
);
842 * To work around bugs in some proxies, don't use Keep-Alive for some
846 if (code
>= HTTP_BAD_REQUEST
)
847 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
850 * Send an error message back to the client. If the error code is a
851 * 400 or 500 series, make sure the message contains some text, too!
854 if (!SendHeader(con
, code
, NULL
))
857 if (code
== HTTP_UNAUTHORIZED
)
859 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
863 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
865 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
869 if (code
>= HTTP_BAD_REQUEST
)
872 * Send a human-readable error message.
875 sprintf(message
, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
876 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
877 code
, httpStatus(code
), httpStatus(code
),
878 con
->language
? (char *)con
->language
->messages
[code
] : httpStatus(code
));
880 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
882 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
884 if (httpPrintf(HTTP(con
), "\r\n") < 0)
886 if (send(con
->http
.fd
, message
, strlen(message
), 0) < 0)
889 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
892 con
->http
.state
= HTTP_WAITING
;
899 * 'SendFile()' - Send a file via HTTP.
903 SendFile(client_t
*con
,
907 struct stat
*filestats
)
909 con
->file
= open(filename
, O_RDONLY
);
911 LogMessage(LOG_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
916 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
920 if (!SendHeader(con
, code
, type
))
923 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
925 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
927 if (httpPrintf(HTTP(con
), "\r\n") < 0)
930 FD_SET(con
->http
.fd
, &OutputSet
);
937 * 'SendHeader()' - Send an HTTP header.
940 int /* O - 1 on success, 0 on failure */
941 SendHeader(client_t
*con
, /* I - Client to send to */
942 http_status_t code
, /* I - HTTP status code */
943 char *type
) /* I - MIME type of document */
945 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
946 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
948 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
950 if (httpPrintf(HTTP(con
), "Server: CUPS/1.0\r\n") < 0)
952 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
954 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
956 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
959 if (con
->language
!= NULL
)
961 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
962 con
->language
->language
) < 0)
966 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
967 cupsLangEncoding(con
->language
)) < 0)
970 else if (type
!= NULL
)
971 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
979 * 'StartListening()' - Create all listening sockets...
985 int i
, /* Looping var */
986 val
; /* Parameter value */
987 listener_t
*lis
; /* Current listening socket */
990 #if defined(WIN32) || defined(__EMX__)
992 * Initialize Windows sockets... This handles loading the DLL, etc. since
993 * Windows doesn't have built-in support for sockets like UNIX...
996 WSADATA wsadata
; /* Socket data for application from DLL */
997 static int initialized
= 0;/* Whether or not we've initialized things */
1004 if (WSAStartup(MAKEWORD(1,1), &wsadata
) != 0)
1009 * Setup a 'broken pipe' signal handler for lost clients.
1012 sigset(SIGPIPE
, sigpipe_handler
);
1016 * Setup socket listeners...
1019 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
1021 LogMessage(LOG_INFO
, "StartListening() address=%08x port=%d",
1022 ntohl(lis
->address
.sin_addr
.s_addr
),
1023 ntohs(lis
->address
.sin_port
));
1025 if ((lis
->fd
= socket(AF_INET
, SOCK_STREAM
, PF_UNSPEC
)) == -1)
1027 LogMessage(LOG_ERROR
, "StartListening() Unable to open listen socket - %s.",
1032 fcntl(lis
->fd
, F_SETFD
, fcntl(lis
->fd
, F_GETFD
) | FD_CLOEXEC
);
1035 * Set things up to reuse the local address for this port.
1039 setsockopt(lis
->fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
1042 * Bind to the port we found...
1045 if (bind(lis
->fd
, (struct sockaddr
*)&(lis
->address
), sizeof(lis
->address
)) < 0)
1047 LogMessage(LOG_ERROR
, "StartListening() Unable to bind socket - %s.", strerror(errno
));
1052 * Listen for new clients.
1055 if (listen(lis
->fd
, SOMAXCONN
) < 0)
1057 LogMessage(LOG_ERROR
, "StartListening() Unable to listen for clients - %s.",
1063 * Setup the select() input mask to contain the listening socket we have.
1066 FD_SET(lis
->fd
, &InputSet
);
1069 LogMessage(LOG_INFO
, "StartListening() NumListeners=%d", NumListeners
);
1074 * 'StopListening()' - Close all listening sockets...
1080 int i
; /* Looping var */
1081 listener_t
*lis
; /* Current listening socket */
1084 for (i
= NumListeners
, lis
= Listeners
; i
> 0; i
--, lis
++)
1086 #if defined(WIN32) || defined(__EMX__)
1087 closesocket(lis
->fd
);
1090 #endif /* WIN32 || __EMX__ */
1092 FD_CLR(lis
->fd
, &InputSet
);
1095 LogMessage(LOG_INFO
, "StopListening()");
1100 * 'WriteClient()' - Write data to a client as needed.
1104 WriteClient(client_t
*con
)
1108 char buf
[HTTP_MAX_BUFFER
];
1111 if (con
->http
.state
!= HTTP_GET_SEND
&&
1112 con
->http
.state
!= HTTP_POST_SEND
)
1115 if (con
->response
!= NULL
)
1116 bytes
= ippWrite(&(con
->http
), con
->response
) != IPP_DATA
;
1117 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
))) > 0)
1119 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1125 con
->bytes
+= bytes
;
1130 LogRequest(con
, HTTP_OK
);
1132 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1134 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1141 FD_CLR(con
->http
.fd
, &OutputSet
);
1142 FD_CLR(con
->file
, &InputSet
);
1146 kill(con
->pipe_pid
, SIGKILL
);
1147 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
1152 if (!con
->http
.keep_alive
)
1158 con
->http
.state
= HTTP_WAITING
;
1162 if (con
->request
!= NULL
)
1164 ippDelete(con
->request
);
1165 con
->request
= NULL
;
1168 if (con
->response
!= NULL
)
1170 ippDelete(con
->response
);
1171 con
->response
= NULL
;
1175 LogMessage(LOG_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1177 con
->http
.activity
= time(NULL
);
1184 * 'decode_basic_auth()' - Decode a Basic authorization string.
1188 decode_basic_auth(client_t
*con
) /* I - Client to decode to */
1190 char value
[1024]; /* Value string */
1194 * Decode the string and pull the username and password out...
1197 httpDecode64(value
, con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
]);
1199 LogMessage(LOG_DEBUG
, "decode_basic_auth() %d Authorization=\"%s\"",
1200 con
->http
.fd
, value
);
1202 sscanf(value
, "%[^:]:%[^\n]", con
->username
, con
->password
);
1207 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1210 static int /* O - 1 if modified since */
1211 check_if_modified(client_t
*con
, /* I - Client connection */
1212 struct stat
*filestats
) /* I - File information */
1214 char *ptr
; /* Pointer into field */
1215 time_t date
; /* Time/date value */
1216 int size
; /* Size/length value */
1221 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1226 LogMessage(LOG_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1229 while (*ptr
!= '\0')
1231 while (isspace(*ptr
) || *ptr
== ';')
1234 if (strncasecmp(ptr
, "length=", 7) == 0)
1239 while (isdigit(*ptr
))
1242 else if (isalpha(*ptr
))
1244 date
= httpGetDateTime(ptr
);
1245 while (*ptr
!= '\0' && *ptr
!= ';')
1250 LogMessage(LOG_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1251 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1253 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1254 (date
< filestats
->st_mtime
&& date
!= 0) ||
1255 (size
== 0 && date
== 0));
1260 * 'get_extension()' - Get the extension for a filename.
1263 static char * /* O - Pointer to extension */
1264 get_extension(char *filename
) /* I - Filename or URI */
1266 char *ext
; /* Pointer to extension */
1269 ext
= filename
+ strlen(filename
) - 1;
1271 while (ext
> filename
&& *ext
!= '/')
1282 * 'get_file()' - Get a filename and state info.
1285 static char * /* O - Real filename */
1286 get_file(client_t
*con
, /* I - Client connection */
1287 struct stat
*filestats
)/* O - File information */
1289 int status
; /* Status of filesystem calls */
1290 char *params
; /* Pointer to parameters in URI */
1291 static char filename
[1024]; /* Filename buffer */
1295 * Need to add DocumentRoot global...
1298 if (con
->language
!= NULL
)
1299 sprintf(filename
, "%s/%s%s", DocumentRoot
, con
->language
->language
,
1302 sprintf(filename
, "%s%s", DocumentRoot
, con
->uri
);
1304 if ((params
= strchr(filename
, '?')) != NULL
)
1308 * Grab the status for this language; if there isn't a language-specific file
1309 * then fallback to the default one...
1312 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1315 * Drop the language prefix and try the current directory...
1318 sprintf(filename
, "%s%s", DocumentRoot
, con
->uri
);
1320 status
= stat(filename
, filestats
);
1324 * If we're found a directory, get the index.html file instead...
1327 if (!status
&& S_ISDIR(filestats
->st_mode
))
1329 if (filename
[strlen(filename
) - 1] == '/')
1330 strcat(filename
, "index.html");
1332 strcat(filename
, "/index.html");
1334 status
= stat(filename
, filestats
);
1337 LogMessage(LOG_DEBUG
, "get_filename() %d filename=%s size=%d",
1338 con
->http
.fd
, filename
, filestats
->st_size
);
1348 * 'get_type()' - Get MIME type from the given extension.
1352 get_type(char *extension
)
1354 if (strcmp(extension
, "html") == 0 || strcmp(extension
, "htm") == 0)
1355 return ("text/html");
1356 else if (strcmp(extension
, "txt") == 0)
1357 return ("text/plain");
1358 else if (strcmp(extension
, "gif") == 0)
1359 return ("image/gif");
1360 else if (strcmp(extension
, "jpg") == 0)
1361 return ("image/jpg");
1362 else if (strcmp(extension
, "png") == 0)
1363 return ("image/png");
1364 else if (strcmp(extension
, "ps") == 0)
1365 return ("application/postscript");
1366 else if (strcmp(extension
, "pdf") == 0)
1367 return ("application/pdf");
1368 else if (strcmp(extension
, "gz") == 0)
1369 return ("application/gzip");
1371 return ("application/unknown");
1376 * 'pipe_command()' - Pipe the output of a command to the remote client.
1379 static int /* O - Process ID */
1380 pipe_command(client_t
*con
, /* I - Client connection */
1381 int infile
, /* I - Standard input for command */
1382 int *outfile
, /* O - Standard output for command */
1383 char *command
, /* I - Command to run */
1384 char *options
) /* I - Options for command */
1386 int pid
; /* Process ID */
1387 char *commptr
; /* Command string pointer */
1388 int fds
[2]; /* Pipe FDs */
1389 int argc
; /* Number of arguments */
1390 char argbuf
[1024], /* Argument buffer */
1391 *argv
[100], /* Argument strings */
1392 *envp
[100]; /* Environment variables */
1393 static char lang
[1024]; /* LANG env variable */
1394 static char content_length
[1024]; /* CONTENT_LENGTH env variable */
1395 static char content_type
[1024]; /* CONTENT_TYPE env variable */
1396 static char server_port
[1024]; /* Default listen port */
1397 static char remote_host
[1024]; /* REMOTE_HOST env variable */
1398 static char remote_user
[1024]; /* REMOTE_HOST env variable */
1402 * Copy the command string...
1405 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1406 argbuf
[sizeof(argbuf
) - 1] = '\0';
1409 * Parse the string; arguments can be separated by spaces or by ? or +...
1414 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0'; commptr
++)
1415 if (*commptr
== ' ' || *commptr
== '?' || *commptr
== '+')
1419 while (*commptr
== ' ')
1422 if (*commptr
!= '\0')
1424 argv
[argc
] = commptr
;
1430 else if (*commptr
== '%')
1432 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1433 *commptr
= (commptr
[1] - '0') << 4;
1435 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1437 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1438 *commptr
|= commptr
[2] - '0';
1440 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1442 strcpy(commptr
+ 1, commptr
+ 3);
1447 if (argv
[1][0] == '\0')
1448 argv
[1] = strrchr(command
, '/') + 1;
1451 * Setup the environment variables as needed...
1454 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1455 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1456 sprintf(remote_host
, "REMOTE_HOST=%s", con
->http
.hostname
);
1457 sprintf(remote_user
, "REMOTE_USER=%s", con
->username
);
1459 envp
[0] = "PATH=/bin:/usr/bin";
1460 envp
[1] = "SERVER_SOFTWARE=CUPS/1.0";
1461 envp
[2] = "SERVER_NAME=localhost"; /* This isn't 100% correct... */
1462 envp
[3] = "GATEWAY_INTERFACE=CGI/1.1";
1463 envp
[4] = "SERVER_PROTOCOL=HTTP/1.1";
1464 envp
[5] = server_port
;
1465 envp
[6] = remote_host
;
1466 envp
[7] = remote_user
;
1470 if (con
->operation
== HTTP_GET
)
1472 envp
[10] = "REQUEST_METHOD=GET";
1477 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->http
.data_remaining
);
1478 sprintf(content_type
, "CONTENT_TYPE=%s",
1479 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1481 envp
[10] = "REQUEST_METHOD=POST";
1482 envp
[11] = content_length
;
1483 envp
[12] = content_type
;
1488 * Create a pipe for the output...
1495 * Then execute the pipe command...
1498 if ((pid
= fork()) == 0)
1501 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1520 * Execute the pipe program; if an error occurs, exit with status 1...
1523 execve(command
, argv
, envp
);
1530 * Error - can't fork!
1540 * Fork successful - return the PID...
1552 * 'show_printer_status()' - Show the current printer status.
1556 show_printer_status(client_t
*con
)
1559 char buffer
[HTTP_MAX_BUFFER
];
1560 static char *states
[] =
1562 "", "", "", "Idle", "Processing", "Stopped"
1567 if (con
->uri
[10] != '\0')
1568 if ((p
= FindPrinter(con
->uri
+ 10)) == NULL
)
1571 if (!SendHeader(con
, HTTP_OK
, "text/html"))
1574 if (con
->http
.version
== HTTP_1_1
)
1576 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
1578 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
1582 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1588 * Send printer information...
1591 sprintf(buffer
, "<HTML>\n"
1593 "\t<TITLE>Printer Status for %s</TITLE>\n"
1594 "\t<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\">\n"
1596 "<BODY BGCOLOR=#ffffff>\n"
1597 "<TABLE WIDTH=100%%><TR>\n"
1598 "\t<TD ALIGN=LEFTV ALIGN=MIDDLE><IMG SRC=\"/images/cups-small.gif\"></TD>\n"
1599 "\t<TD ALIGN=CENTER VALIGN=MIDDLE><H1>Printer Status for %s</H1></TD>\n"
1600 "\t<TD ALIGN=RIGHT VALIGN=MIDDLE><IMG SRC=\"/images/vendor.gif\"></TD>\n"
1602 "<CENTER><TABLE BORDER=1 WIDTH=80%%>\n"
1603 "<TR><TH>Name</TH><TH>Value</TH></TR>\n"
1604 "<TR><TD>Info</TD><TD>%s</TD></TR>\n"
1605 "<TR><TD>MoreInfo</TD><TD>%s</TD></TR>\n"
1606 "<TR><TD>Location</TD><TD>%s</TD></TR>\n"
1607 "<TR><TD>Device URI</TD><TD>%s</TD></TR>\n"
1608 "<TR><TD>PPDFile</TD><TD>%s</TD></TR>\n",
1609 p
->name
, p
->name
, p
->info
, p
->more_info
,
1610 p
->location
, p
->device_uri
, p
->ppd
);
1612 con
->bytes
+= httpWrite(HTTP(con
), buffer
, strlen(buffer
));
1616 strcpy(buffer
, "<HTML>\n"
1618 "\t<TITLE>Available Printers</TITLE>\n"
1619 "\t<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\">\n"
1621 "<BODY BGCOLOR=#ffffff>\n"
1622 "<TABLE WIDTH=100%%><TR>\n"
1623 "\t<TD ALIGN=LEFT VALIGN=MIDDLE><IMG SRC=\"/images/cups-small.gif\"></TD>\n"
1624 "\t<TD ALIGN=CENTER VALIGN=MIDDLE><H1>Available Printers</H1></TD>\n"
1625 "\t<TD ALIGN=RIGHT VALIGN=MIDDLE><IMG SRC=\"/images/vendor.gif\"></TD>\n"
1627 "<CENTER><TABLE BORDER=1 WIDTH=80%%>\n"
1628 "<TR><TH>Name</TH><TH>State</TH><TH>Info</TH></TR>\n");
1630 con
->bytes
+= httpWrite(HTTP(con
), buffer
, strlen(buffer
));
1632 for (p
= Printers
; p
!= NULL
; p
= p
->next
)
1634 sprintf(buffer
, "<TR><TD><A HREF=/printers/%s>%s</A></TD><TD>%s</TD><TD>%s</TD></TR>\n",
1635 p
->name
, p
->name
, states
[p
->state
], p
->info
);
1636 con
->bytes
+= httpWrite(HTTP(con
), buffer
, strlen(buffer
));
1640 strcpy(buffer
, "</TABLE></CENTER>\n"
1642 "CUPS Copyright 1997-1999 by Easy Software Products, "
1643 "All Rights Reserved. CUPS and the CUPS logo are the "
1644 "trademark property of Easy Software Products.\n"
1647 con
->bytes
+= httpWrite(HTTP(con
), buffer
, strlen(buffer
));
1648 httpWrite(HTTP(con
), buffer
, 0);
1655 * 'sigpipe_handler()' - Handle 'broken pipe' signals from lost network
1660 sigpipe_handler(int sig
) /* I - Signal number */
1667 * End of "$Id: client.c,v 1.4 1999/02/19 22:07:04 mike Exp $".