2 * "$Id: client.c,v 1.38 1999/10/22 18:30:18 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 * 44141 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 * WriteClient() - Write data to a client as needed.
35 * check_if_modified() - Decode an "If-Modified-Since" line.
36 * decode_basic_auth() - Decode a Basic authorization string.
37 * get_file() - Get a filename and state info.
38 * pipe_command() - Pipe the output of a command to the remote client.
42 * Include necessary headers...
52 static int check_if_modified(client_t
*con
, struct stat
*filestats
);
53 static void decode_basic_auth(client_t
*con
);
54 static char *get_file(client_t
*con
, struct stat
*filestats
);
55 static int pipe_command(client_t
*con
, int infile
, int *outfile
, char *command
, char *options
);
59 * 'AcceptClient()' - Accept a new client.
63 AcceptClient(listener_t
*lis
) /* I - Listener socket */
65 int i
; /* Looping var */
66 int val
; /* Parameter value */
67 client_t
*con
; /* New client pointer */
68 unsigned address
;/* Address of client */
69 struct hostent
*host
; /* Host entry for address */
72 DEBUG_printf(("AcceptClient(%08x) %d NumClients = %d\n",
73 lis
, lis
->fd
, NumClients
));
76 * Get a pointer to the next available client...
79 con
= Clients
+ NumClients
;
81 memset(con
, 0, sizeof(client_t
));
82 con
->http
.activity
= time(NULL
);
85 * Accept the client and get the remote address...
88 val
= sizeof(struct sockaddr_in
);
90 if ((con
->http
.fd
= accept(lis
->fd
, (struct sockaddr
*)&(con
->http
.hostaddr
),
93 LogMessage(LOG_ERROR
, "accept() failed - %s.", strerror(errno
));
97 con
->http
.hostaddr
.sin_port
= lis
->address
.sin_port
;
100 * Get the hostname or format the IP address as needed...
103 address
= ntohl(con
->http
.hostaddr
.sin_addr
.s_addr
);
107 host
= gethostbyaddr((char *)&address
, sizeof(address
), AF_INET
);
109 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_DEBUG
, "accept() %d from %s:%d.", con
->http
.fd
,
121 con
->http
.hostname
, ntohs(con
->http
.hostaddr
.sin_port
));
124 * Add the socket to the select() input mask.
127 fcntl(con
->http
.fd
, F_SETFD
, fcntl(con
->http
.fd
, F_GETFD
) | FD_CLOEXEC
);
129 DEBUG_printf(("AcceptClient: Adding fd %d to InputSet...\n", con
->http
.fd
));
130 FD_SET(con
->http
.fd
, &InputSet
);
135 * Temporarily suspend accept()'s until we lose a client...
138 if (NumClients
== MaxClients
)
139 for (i
= 0; i
< NumListeners
; i
++)
141 DEBUG_printf(("AcceptClient: Removing fd %d from InputSet...\n", Listeners
[i
].fd
));
142 FD_CLR(Listeners
[i
].fd
, &InputSet
);
148 * 'CloseAllClients()' - Close all remote clients immediately.
152 CloseAllClients(void)
154 while (NumClients
> 0)
155 CloseClient(Clients
);
160 * 'CloseClient()' - Close a remote client.
164 CloseClient(client_t
*con
) /* I - Client to close */
166 int i
; /* Looping var */
167 int status
; /* Exit status of pipe command */
170 LogMessage(LOG_DEBUG
, "CloseClient() %d", con
->http
.fd
);
173 * Close the socket and clear the file from the input set for select()...
176 if (con
->http
.fd
> 0)
178 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->http
.fd
));
180 FD_CLR(con
->http
.fd
, &InputSet
);
181 FD_CLR(con
->http
.fd
, &OutputSet
);
185 for (i
= 0; i
< NumListeners
; i
++)
187 DEBUG_printf(("CloseClient: Adding fd %d to InputSet...\n", Listeners
[i
].fd
));
188 FD_SET(Listeners
[i
].fd
, &InputSet
);
191 if (con
->pipe_pid
!= 0)
193 DEBUG_printf(("CloseClient: Removing fd %d from InputSet...\n", con
->file
));
194 FD_CLR(con
->file
, &InputSet
);
198 * If we have a data file open, close it...
205 kill(con
->pipe_pid
, SIGKILL
);
206 waitpid(con
->pipe_pid
, &status
, WNOHANG
);
209 FD_CLR(con
->file
, &InputSet
);
215 * Compact the list of clients as necessary...
220 if (con
< (Clients
+ NumClients
))
221 memcpy(con
, con
+ 1, (Clients
+ NumClients
- con
) * sizeof(client_t
));
226 * 'ReadClient()' - Read data from a client.
229 int /* O - 1 on success, 0 on error */
230 ReadClient(client_t
*con
) /* I - Client to read from */
232 char line
[8192], /* Line from client... */
233 operation
[64], /* Operation code from socket */
234 version
[64]; /* HTTP version number string */
235 int major
, minor
; /* HTTP version numbers */
236 http_status_t status
; /* Transfer status */
237 ipp_state_t ipp_state
; /* State of IPP transfer */
238 int bytes
; /* Number of bytes to POST */
239 char *filename
; /* Name of file for GET/HEAD */
240 struct stat filestats
; /* File information */
241 mime_type_t
*type
; /* MIME type of file */
242 char command
[1024], /* Command to run */
243 *options
; /* Options/CGI data */
244 printer_t
*p
; /* Printer */
247 status
= HTTP_CONTINUE
;
249 switch (con
->http
.state
)
253 * See if we've received a request line...
256 if (httpGets(line
, sizeof(line
) - 1, HTTP(con
)) == NULL
)
263 * Ignore blank request lines...
270 * Clear other state variables...
273 httpClearFields(HTTP(con
));
275 con
->http
.activity
= time(NULL
);
276 con
->http
.version
= HTTP_1_0
;
277 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
278 con
->http
.data_encoding
= HTTP_ENCODE_LENGTH
;
279 con
->http
.data_remaining
= 0;
280 con
->operation
= HTTP_WAITING
;
284 con
->username
[0] = '\0';
285 con
->password
[0] = '\0';
288 if (con
->language
!= NULL
)
290 cupsLangFree(con
->language
);
291 con
->language
= NULL
;
295 * Grab the request line...
298 switch (sscanf(line
, "%63s%1023s%63s", operation
, con
->uri
, version
))
301 SendError(con
, HTTP_BAD_REQUEST
);
305 con
->http
.version
= HTTP_0_9
;
308 if (sscanf(version
, "HTTP/%d.%d", &major
, &minor
) != 2)
310 SendError(con
, HTTP_BAD_REQUEST
);
317 con
->http
.version
= (http_version_t
)(major
* 100 + minor
);
318 if (con
->http
.version
== HTTP_1_1
)
319 con
->http
.keep_alive
= HTTP_KEEPALIVE_ON
;
321 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
325 SendError(con
, HTTP_NOT_SUPPORTED
);
333 * Process the request...
336 if (strcmp(operation
, "GET") == 0)
337 con
->http
.state
= HTTP_GET
;
338 else if (strcmp(operation
, "PUT") == 0)
339 con
->http
.state
= HTTP_PUT
;
340 else if (strcmp(operation
, "POST") == 0)
341 con
->http
.state
= HTTP_POST
;
342 else if (strcmp(operation
, "DELETE") == 0)
343 con
->http
.state
= HTTP_DELETE
;
344 else if (strcmp(operation
, "TRACE") == 0)
345 con
->http
.state
= HTTP_TRACE
;
346 else if (strcmp(operation
, "CLOSE") == 0)
347 con
->http
.state
= HTTP_CLOSE
;
348 else if (strcmp(operation
, "OPTIONS") == 0)
349 con
->http
.state
= HTTP_OPTIONS
;
350 else if (strcmp(operation
, "HEAD") == 0)
351 con
->http
.state
= HTTP_HEAD
;
354 SendError(con
, HTTP_BAD_REQUEST
);
359 con
->start
= time(NULL
);
360 con
->operation
= con
->http
.state
;
362 LogMessage(LOG_DEBUG
, "ReadClient() %d %s %s HTTP/%d.%d", con
->http
.fd
,
364 con
->http
.version
/ 100, con
->http
.version
% 100);
366 con
->http
.status
= HTTP_OK
;
377 * Parse incoming parameters until the status changes...
380 status
= httpUpdate(HTTP(con
));
382 if (status
!= HTTP_OK
&& status
!= HTTP_CONTINUE
)
384 SendError(con
, HTTP_BAD_REQUEST
);
392 * Handle new transfers...
395 if (status
== HTTP_OK
)
397 con
->language
= cupsLangGet(con
->http
.fields
[HTTP_FIELD_ACCEPT_LANGUAGE
]);
399 decode_basic_auth(con
);
401 if (con
->http
.fields
[HTTP_FIELD_HOST
][0] == '\0' &&
402 con
->http
.version
>= HTTP_1_0
)
404 if (!SendError(con
, HTTP_BAD_REQUEST
))
410 else if (strncmp(con
->uri
, "..", 2) == 0)
413 * Protect against malicious users!
416 if (!SendError(con
, HTTP_FORBIDDEN
))
422 else if (con
->uri
[0] != '/')
425 * Don't allow proxying (yet)...
428 if (!SendError(con
, HTTP_METHOD_NOT_ALLOWED
))
434 else if ((status
= IsAuthorized(con
)) != HTTP_OK
)
436 SendError(con
, status
);
440 else switch (con
->http
.state
)
443 if (strncmp(con
->uri
, "/printers/", 10) == 0 &&
444 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
447 * Send PPD file - get the real printer name since printer
448 * names are not case sensitive but filename can be...
451 con
->uri
[strlen(con
->uri
) - 4] = '\0'; /* Drop ".ppd" */
453 if ((p
= FindPrinter(con
->uri
+ 10)) != NULL
)
454 sprintf(con
->uri
, "/ppd/%s.ppd", p
->name
);
457 if (!SendError(con
, HTTP_NOT_FOUND
))
467 if (strncmp(con
->uri
, "/printers", 9) == 0 ||
468 strncmp(con
->uri
, "/classes", 8) == 0 ||
469 strncmp(con
->uri
, "/jobs", 5) == 0)
475 if (strncmp(con
->uri
, "/printers", 9) == 0)
477 snprintf(command
, sizeof(command
), "%s/cgi-bin/printers.cgi", ServerRoot
);
478 options
= con
->uri
+ 9;
480 else if (strncmp(con
->uri
, "/classes", 8) == 0)
482 snprintf(command
, sizeof(command
), "%s/cgi-bin/classes.cgi", ServerRoot
);
483 options
= con
->uri
+ 8;
487 snprintf(command
, sizeof(command
), "%s/cgi-bin/jobs.cgi", ServerRoot
);
488 options
= con
->uri
+ 5;
494 if (!SendCommand(con
, command
, options
))
496 if (!SendError(con
, HTTP_NOT_FOUND
))
503 LogRequest(con
, HTTP_OK
);
505 if (con
->http
.version
<= HTTP_1_0
)
506 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
514 if ((filename
= get_file(con
, &filestats
)) == NULL
)
516 if (!SendError(con
, HTTP_NOT_FOUND
))
522 else if (!check_if_modified(con
, &filestats
))
524 if (!SendError(con
, HTTP_NOT_MODIFIED
))
532 type
= mimeFileType(MimeDatabase
, filename
);
534 strcpy(line
, "text/plain");
536 sprintf(line
, "%s/%s", type
->super
, type
->type
);
538 if (!SendFile(con
, HTTP_OK
, filename
, line
, &filestats
))
547 case HTTP_POST_RECV
:
549 * See if the POST request includes a Content-Length field, and if
550 * so check the length against any limits that are set...
553 if (con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
][0] &&
554 atoi(con
->http
.fields
[HTTP_FIELD_CONTENT_LENGTH
]) > MaxRequestSize
&&
558 * Request too large...
561 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
571 * See what kind of POST request this is; for IPP requests the
572 * content-type field will be "application/ipp"...
575 if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/ipp") == 0)
576 con
->request
= ippNew();
577 else if (strcmp(con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
], "application/x-www-form-urlencoded") == 0 &&
578 (strncmp(con
->uri
, "/printers", 9) == 0 ||
579 strncmp(con
->uri
, "/classes", 8) == 0 ||
580 strncmp(con
->uri
, "/jobs", 5) == 0))
586 if (strncmp(con
->uri
, "/printers", 9) == 0)
588 snprintf(command
, sizeof(command
), "%s/cgi-bin/printers", ServerRoot
);
589 options
= con
->uri
+ 9;
591 else if (strncmp(con
->uri
, "/classes", 8) == 0)
593 snprintf(command
, sizeof(command
), "%s/cgi-bin/classes", ServerRoot
);
594 options
= con
->uri
+ 8;
598 snprintf(command
, sizeof(command
), "%s/cgi-bin/jobs", ServerRoot
);
599 options
= con
->uri
+ 5;
605 if (!SendCommand(con
, command
, options
))
607 if (!SendError(con
, HTTP_NOT_FOUND
))
614 LogRequest(con
, HTTP_OK
);
616 if (con
->http
.version
<= HTTP_1_0
)
617 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
619 else if (!SendError(con
, HTTP_UNAUTHORIZED
))
629 SendError(con
, HTTP_NOT_IMPLEMENTED
);
636 if (strncmp(con
->uri
, "/printers", 9) == 0 &&
637 strcmp(con
->uri
+ strlen(con
->uri
) - 4, ".ppd") == 0)
643 snprintf(command
, sizeof(command
), "/ppd/%s", con
->uri
+ 10);
644 strcpy(con
->uri
, command
);
647 if (strncmp(con
->uri
, "/printers/", 10) == 0 ||
648 strncmp(con
->uri
, "/classes/", 9) == 0 ||
649 strncmp(con
->uri
, "/jobs/", 6) == 0)
655 if (!SendHeader(con
, HTTP_OK
, "text/html"))
661 if (httpPrintf(HTTP(con
), "\r\n") < 0)
667 LogRequest(con
, HTTP_OK
);
669 else if ((filename
= get_file(con
, &filestats
)) == NULL
)
671 if (!SendHeader(con
, HTTP_NOT_FOUND
, "text/html"))
677 LogRequest(con
, HTTP_NOT_FOUND
);
679 else if (!check_if_modified(con
, &filestats
))
681 if (!SendError(con
, HTTP_NOT_MODIFIED
))
687 LogRequest(con
, HTTP_NOT_MODIFIED
);
695 type
= mimeFileType(MimeDatabase
, filename
);
697 strcpy(line
, "text/plain");
699 sprintf(line
, "%s/%s", type
->super
, type
->type
);
701 if (!SendHeader(con
, HTTP_OK
, line
))
707 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n",
708 httpGetDateString(filestats
.st_mtime
)) < 0)
714 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n",
715 filestats
.st_size
) < 0)
721 LogRequest(con
, HTTP_OK
);
724 if (httpPrintf(HTTP(con
), "\r\n") < 0)
730 con
->http
.state
= HTTP_WAITING
;
736 * Handle any incoming data...
739 switch (con
->http
.state
)
744 case HTTP_POST_RECV
:
745 LogMessage(LOG_DEBUG
, "ReadClient() %d con->data_encoding = %s con->data_remaining = %d",
747 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
748 con
->http
.data_remaining
);
749 DEBUG_printf(("ReadClient() %d con->data_encoding = %s con->data_remaining = %d\n",
751 con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
? "chunked" : "length",
752 con
->http
.data_remaining
));
754 if (con
->request
!= NULL
)
757 * Grab any request data from the connection...
760 if ((ipp_state
= ippRead(&(con
->http
), con
->request
)) == IPP_ERROR
)
762 LogMessage(LOG_ERROR
, "ReadClient() %d IPP Read Error!",
767 else if (ipp_state
!= IPP_DATA
)
770 if (con
->file
== 0 && con
->http
.state
!= HTTP_POST_SEND
)
773 * Create a file as needed for the request data...
776 snprintf(con
->filename
, sizeof(con
->filename
), "%s/requests/XXXXXX", ServerRoot
);
777 con
->file
= mkstemp(con
->filename
);
778 fchmod(con
->file
, 0640);
780 LogMessage(LOG_DEBUG
, "ReadClient() %d REQUEST %s", con
->http
.fd
,
785 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
794 if (con
->http
.state
!= HTTP_POST_SEND
)
796 if ((bytes
= httpRead(HTTP(con
), line
, sizeof(line
))) < 0)
806 LogMessage(LOG_DEBUG
, "ReadClient() %d writing %d bytes", bytes
);
808 if (write(con
->file
, line
, bytes
) < bytes
)
812 unlink(con
->filename
);
814 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
821 else if (con
->http
.state
!= HTTP_POST_SEND
)
828 if (con
->http
.state
== HTTP_POST_SEND
)
832 fstat(con
->file
, &filestats
);
836 if (filestats
.st_size
> MaxRequestSize
&&
840 * Request is too big; remove it and send an error...
843 unlink(con
->filename
);
848 * Delete any IPP request data...
851 ippDelete(con
->request
);
855 if (!SendError(con
, HTTP_REQUEST_TOO_LARGE
))
864 ProcessIPPRequest(con
);
869 if (!con
->http
.keep_alive
&& con
->http
.state
== HTTP_WAITING
)
880 * 'SendCommand()' - Send output from a command via HTTP.
884 SendCommand(client_t
*con
,
888 con
->pipe_pid
= pipe_command(con
, 0, &(con
->file
), command
, options
);
890 LogMessage(LOG_DEBUG
, "SendCommand() %d command=\"%s\" file=%d pipe_pid=%d",
891 con
->http
.fd
, command
, con
->file
, con
->pipe_pid
);
893 if (con
->pipe_pid
== 0)
896 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
898 DEBUG_printf(("SendCommand: Adding fd %d to InputSet...\n", con
->file
));
899 FD_SET(con
->file
, &InputSet
);
900 FD_SET(con
->http
.fd
, &OutputSet
);
902 if (!SendHeader(con
, HTTP_OK
, NULL
))
905 if (con
->http
.version
== HTTP_1_1
)
907 con
->http
.data_encoding
= HTTP_ENCODE_CHUNKED
;
909 if (httpPrintf(HTTP(con
), "Transfer-Encoding: chunked\r\n") < 0)
918 * 'SendError()' - Send an error message via HTTP.
921 int /* O - 1 if successful, 0 otherwise */
922 SendError(client_t
*con
, /* I - Connection */
923 http_status_t code
) /* I - Error code */
925 char message
[1024]; /* Message for user */
929 * Put the request in the access_log file...
932 if (con
->operation
> HTTP_WAITING
)
933 LogRequest(con
, code
);
936 * To work around bugs in some proxies, don't use Keep-Alive for some
940 if (code
>= HTTP_BAD_REQUEST
)
941 con
->http
.keep_alive
= HTTP_KEEPALIVE_OFF
;
944 * Send an error message back to the client. If the error code is a
945 * 400 or 500 series, make sure the message contains some text, too!
948 if (!SendHeader(con
, code
, NULL
))
951 if (code
== HTTP_UNAUTHORIZED
)
953 if (httpPrintf(HTTP(con
), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0)
957 if (con
->http
.version
>= HTTP_1_1
&& !con
->http
.keep_alive
)
959 if (httpPrintf(HTTP(con
), "Connection: close\r\n") < 0)
963 if (code
>= HTTP_BAD_REQUEST
)
966 * Send a human-readable error message.
969 snprintf(message
, sizeof(message
),
970 "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>"
971 "<BODY><H1>%s</H1>%s</BODY></HTML>\n",
972 code
, httpStatus(code
), httpStatus(code
),
973 con
->language
? con
->language
->messages
[code
] :
976 if (httpPrintf(HTTP(con
), "Content-Type: text/html\r\n") < 0)
978 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", strlen(message
)) < 0)
980 if (httpPrintf(HTTP(con
), "\r\n") < 0)
982 if (httpPrintf(HTTP(con
), "%s", message
) < 0)
985 else if (httpPrintf(HTTP(con
), "\r\n") < 0)
988 con
->http
.state
= HTTP_WAITING
;
995 * 'SendFile()' - Send a file via HTTP.
999 SendFile(client_t
*con
,
1003 struct stat
*filestats
)
1005 con
->file
= open(filename
, O_RDONLY
);
1007 LogMessage(LOG_DEBUG
, "SendFile() %d file=%d", con
->http
.fd
, con
->file
);
1012 fcntl(con
->file
, F_SETFD
, fcntl(con
->file
, F_GETFD
) | FD_CLOEXEC
);
1016 if (!SendHeader(con
, code
, type
))
1019 if (httpPrintf(HTTP(con
), "Last-Modified: %s\r\n", httpGetDateString(filestats
->st_mtime
)) < 0)
1021 if (httpPrintf(HTTP(con
), "Content-Length: %d\r\n", filestats
->st_size
) < 0)
1023 if (httpPrintf(HTTP(con
), "\r\n") < 0)
1026 FD_SET(con
->http
.fd
, &OutputSet
);
1033 * 'SendHeader()' - Send an HTTP request.
1036 int /* O - 1 on success, 0 on failure */
1037 SendHeader(client_t
*con
, /* I - Client to send to */
1038 http_status_t code
, /* I - HTTP status code */
1039 char *type
) /* I - MIME type of document */
1041 if (httpPrintf(HTTP(con
), "HTTP/%d.%d %d %s\r\n", con
->http
.version
/ 100,
1042 con
->http
.version
% 100, code
, httpStatus(code
)) < 0)
1044 if (httpPrintf(HTTP(con
), "Date: %s\r\n", httpGetDateString(time(NULL
))) < 0)
1046 if (httpPrintf(HTTP(con
), "Server: CUPS/1.0\r\n") < 0)
1048 if (con
->http
.keep_alive
&& con
->http
.version
>= HTTP_1_0
)
1050 if (httpPrintf(HTTP(con
), "Connection: Keep-Alive\r\n") < 0)
1052 if (httpPrintf(HTTP(con
), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout
) < 0)
1055 if (con
->language
!= NULL
)
1057 if (httpPrintf(HTTP(con
), "Content-Language: %s\r\n",
1058 con
->language
->language
) < 0)
1062 if (httpPrintf(HTTP(con
), "Content-Type: %s; charset=%s\r\n", type
,
1063 cupsLangEncoding(con
->language
)) < 0)
1066 else if (type
!= NULL
)
1067 if (httpPrintf(HTTP(con
), "Content-Type: %s\r\n", type
) < 0)
1075 * 'WriteClient()' - Write data to a client as needed.
1078 int /* O - 1 if success, 0 if fail */
1079 WriteClient(client_t
*con
) /* I - Client connection */
1081 int bytes
; /* Number of bytes written */
1082 char buf
[HTTP_MAX_BUFFER
]; /* Data buffer */
1083 ipp_state_t ipp_state
; /* IPP state value */
1086 if (con
->http
.state
!= HTTP_GET_SEND
&&
1087 con
->http
.state
!= HTTP_POST_SEND
)
1090 if (con
->response
!= NULL
)
1092 ipp_state
= ippWrite(&(con
->http
), con
->response
);
1093 bytes
= ipp_state
!= IPP_ERROR
&& ipp_state
!= IPP_DATA
;
1095 else if ((bytes
= read(con
->file
, buf
, sizeof(buf
))) > 0)
1097 if (httpWrite(HTTP(con
), buf
, bytes
) < 0)
1103 con
->bytes
+= bytes
;
1108 LogRequest(con
, HTTP_OK
);
1110 if (con
->http
.data_encoding
== HTTP_ENCODE_CHUNKED
)
1112 if (httpPrintf(HTTP(con
), "0\r\n\r\n") < 0)
1119 con
->http
.state
= HTTP_WAITING
;
1121 FD_CLR(con
->http
.fd
, &OutputSet
);
1125 DEBUG_printf(("WriteClient: Removing fd %d from InputSet...\n", con
->file
));
1126 FD_CLR(con
->file
, &InputSet
);
1129 kill(con
->pipe_pid
, SIGTERM
);
1136 if (con
->request
!= NULL
)
1138 ippDelete(con
->request
);
1139 con
->request
= NULL
;
1142 if (con
->response
!= NULL
)
1144 ippDelete(con
->response
);
1145 con
->response
= NULL
;
1148 if (!con
->http
.keep_alive
)
1156 LogMessage(LOG_DEBUG
, "WriteClient() %d %d bytes", con
->http
.fd
, bytes
);
1158 con
->http
.activity
= time(NULL
);
1165 * 'check_if_modified()' - Decode an "If-Modified-Since" line.
1168 static int /* O - 1 if modified since */
1169 check_if_modified(client_t
*con
, /* I - Client connection */
1170 struct stat
*filestats
) /* I - File information */
1172 char *ptr
; /* Pointer into field */
1173 time_t date
; /* Time/date value */
1174 int size
; /* Size/length value */
1179 ptr
= con
->http
.fields
[HTTP_FIELD_IF_MODIFIED_SINCE
];
1184 LogMessage(LOG_DEBUG
, "check_if_modified() %d If-Modified-Since=\"%s\"",
1187 while (*ptr
!= '\0')
1189 while (isspace(*ptr
) || *ptr
== ';')
1192 if (strncasecmp(ptr
, "length=", 7) == 0)
1197 while (isdigit(*ptr
))
1200 else if (isalpha(*ptr
))
1202 date
= httpGetDateTime(ptr
);
1203 while (*ptr
!= '\0' && *ptr
!= ';')
1208 LogMessage(LOG_DEBUG
, "check_if_modified() %d sizes=%d,%d dates=%d,%d",
1209 con
->http
.fd
, size
, filestats
->st_size
, date
, filestats
->st_mtime
);
1211 return ((size
!= filestats
->st_size
&& size
!= 0) ||
1212 (date
< filestats
->st_mtime
&& date
!= 0) ||
1213 (size
== 0 && date
== 0));
1218 * 'decode_basic_auth()' - Decode a Basic authorization string.
1222 decode_basic_auth(client_t
*con
) /* I - Client to decode to */
1224 char *s
, /* Authorization string */
1225 value
[1024]; /* Value string */
1229 * Decode the string and pull the username and password out...
1232 s
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
1233 if (strncmp(s
, "Basic", 5) != 0)
1240 httpDecode64(value
, s
);
1242 sscanf(value
, "%31[^:]:%31s", con
->username
, con
->password
);
1244 LogMessage(LOG_DEBUG
, "decode_basic_auth() %d username=\"%s\"",
1245 con
->http
.fd
, con
->username
);
1250 * 'get_file()' - Get a filename and state info.
1253 static char * /* O - Real filename */
1254 get_file(client_t
*con
, /* I - Client connection */
1255 struct stat
*filestats
)/* O - File information */
1257 int status
; /* Status of filesystem calls */
1258 char *params
; /* Pointer to parameters in URI */
1259 static char filename
[1024]; /* Filename buffer */
1263 * Need to add DocumentRoot global...
1266 if (strncmp(con
->uri
, "/ppd/", 5) == 0)
1267 snprintf(filename
, sizeof(filename
), "%s%s", ServerRoot
, con
->uri
);
1268 else if (con
->language
!= NULL
)
1269 snprintf(filename
, sizeof(filename
), "%s/%s%s", DocumentRoot
, con
->language
->language
,
1272 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1274 if ((params
= strchr(filename
, '?')) != NULL
)
1278 * Grab the status for this language; if there isn't a language-specific file
1279 * then fallback to the default one...
1282 if ((status
= stat(filename
, filestats
)) != 0 && con
->language
!= NULL
)
1285 * Drop the language prefix and try the current directory...
1288 if (strncmp(con
->uri
, "/ppd/", 5) != 0)
1290 snprintf(filename
, sizeof(filename
), "%s%s", DocumentRoot
, con
->uri
);
1292 status
= stat(filename
, filestats
);
1297 * If we're found a directory, get the index.html file instead...
1300 if (!status
&& S_ISDIR(filestats
->st_mode
))
1302 if (filename
[strlen(filename
) - 1] == '/')
1303 strcat(filename
, "index.html");
1305 strcat(filename
, "/index.html");
1307 status
= stat(filename
, filestats
);
1310 LogMessage(LOG_DEBUG
, "get_file() %d filename=%s size=%d",
1311 con
->http
.fd
, filename
, status
? -1 : filestats
->st_size
);
1321 * 'pipe_command()' - Pipe the output of a command to the remote client.
1324 static int /* O - Process ID */
1325 pipe_command(client_t
*con
, /* I - Client connection */
1326 int infile
, /* I - Standard input for command */
1327 int *outfile
, /* O - Standard output for command */
1328 char *command
, /* I - Command to run */
1329 char *options
) /* I - Options for command */
1331 int pid
; /* Process ID */
1332 char *commptr
; /* Command string pointer */
1333 int fds
[2]; /* Pipe FDs */
1334 int argc
; /* Number of arguments */
1335 char argbuf
[1024], /* Argument buffer */
1336 *argv
[100], /* Argument strings */
1337 *envp
[100]; /* Environment variables */
1338 char hostname
[1024]; /* Hostname string */
1339 static char lang
[1024]; /* LANG env variable */
1340 static char content_length
[1024]; /* CONTENT_LENGTH env variable */
1341 static char content_type
[1024]; /* CONTENT_TYPE env variable */
1342 static char ipp_port
[1024]; /* Default listen port */
1343 static char server_port
[1024]; /* Default listen port */
1344 static char server_name
[1024]; /* Default listen hostname */
1345 static char remote_host
[1024]; /* REMOTE_HOST env variable */
1346 static char remote_user
[1024]; /* REMOTE_HOST env variable */
1347 static char tmpdir
[1024]; /* TMPDIR env variable */
1351 * Copy the command string...
1354 strncpy(argbuf
, options
, sizeof(argbuf
) - 1);
1355 argbuf
[sizeof(argbuf
) - 1] = '\0';
1358 * Parse the string; arguments can be separated by spaces or by ? or +...
1363 for (commptr
= argbuf
, argc
= 1; *commptr
!= '\0' && argc
< 99; commptr
++)
1364 if (*commptr
== ' ' || *commptr
== '?' || *commptr
== '+')
1368 while (*commptr
== ' ')
1371 if (*commptr
!= '\0')
1373 argv
[argc
] = commptr
;
1379 else if (*commptr
== '%')
1381 if (commptr
[1] >= '0' && commptr
[1] <= '9')
1382 *commptr
= (commptr
[1] - '0') << 4;
1384 *commptr
= (tolower(commptr
[1]) - 'a' + 10) << 4;
1386 if (commptr
[2] >= '0' && commptr
[2] <= '9')
1387 *commptr
|= commptr
[2] - '0';
1389 *commptr
|= tolower(commptr
[2]) - 'a' + 10;
1391 strcpy(commptr
+ 1, commptr
+ 3);
1396 if (argv
[0][0] == '\0')
1397 argv
[0] = strrchr(command
, '/') + 1;
1400 * Setup the environment variables as needed...
1403 gethostname(hostname
, sizeof(hostname
) - 1);
1405 sprintf(lang
, "LANG=%s", con
->language
? con
->language
->language
: "C");
1406 sprintf(ipp_port
, "IPP_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1407 sprintf(server_port
, "SERVER_PORT=%d", ntohs(con
->http
.hostaddr
.sin_port
));
1408 sprintf(server_name
, "SERVER_NAME=%s", hostname
);
1409 sprintf(remote_host
, "REMOTE_HOST=%s", con
->http
.hostname
);
1410 sprintf(remote_user
, "REMOTE_USER=%s", con
->username
);
1411 sprintf(tmpdir
, "TMPDIR=%s", TempDir
);
1413 envp
[0] = "PATH=/bin:/usr/bin";
1414 envp
[1] = "SERVER_SOFTWARE=CUPS/1.0";
1415 envp
[2] = "GATEWAY_INTERFACE=CGI/1.1";
1416 envp
[3] = "SERVER_PROTOCOL=HTTP/1.1";
1418 envp
[5] = server_name
;
1419 envp
[6] = server_port
;
1420 envp
[7] = remote_host
;
1421 envp
[8] = remote_user
;
1426 if (con
->operation
== HTTP_GET
)
1428 envp
[12] = "REQUEST_METHOD=GET";
1433 sprintf(content_length
, "CONTENT_LENGTH=%d", con
->http
.data_remaining
);
1434 snprintf(content_type
, sizeof(content_type
), "CONTENT_TYPE=%s",
1435 con
->http
.fields
[HTTP_FIELD_CONTENT_TYPE
]);
1437 envp
[12] = "REQUEST_METHOD=POST";
1438 envp
[13] = content_length
;
1439 envp
[14] = content_type
;
1444 * Create a pipe for the output...
1449 LogMessage(LOG_ERROR
, "Unable to create pipes for CGI %s - %s",
1450 argv
[0], strerror(errno
));
1455 * Then execute the command...
1458 if ((pid
= fork()) == 0)
1461 * Child comes here... Close stdin if necessary and dup the pipe to stdout.
1480 * Execute the pipe program; if an error occurs, exit with status 1...
1483 execve(command
, argv
, envp
);
1484 perror("execve failed");
1491 * Error - can't fork!
1494 LogMessage(LOG_ERROR
, "Unable to fork for CGI %s - %s", argv
[0],
1504 * Fork successful - return the PID...
1507 LogMessage(LOG_DEBUG
, "CGI %s started - PID = %d", argv
[0], pid
);
1518 * End of "$Id: client.c,v 1.38 1999/10/22 18:30:18 mike Exp $".