📄 client.c
字号:
{ con->file = open(filename, O_RDONLY); LogMessage(L_DEBUG, "SendFile: %d file=%d", con->http.fd, con->file); if (con->file < 0) return (0); fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC); con->pipe_pid = 0; if (!SendHeader(con, code, type)) return (0); if (httpPrintf(HTTP(con), "Last-Modified: %s\r\n", httpGetDateString(filestats->st_mtime)) < 0) return (0); if (httpPrintf(HTTP(con), "Content-Length: %lu\r\n", (unsigned long)filestats->st_size) < 0) return (0); if (httpPrintf(HTTP(con), "\r\n") < 0) return (0); LogMessage(L_DEBUG2, "SendFile: Adding fd %d to OutputSet...", con->http.fd); FD_SET(con->http.fd, OutputSet); return (1);}/* * 'SendHeader()' - Send an HTTP request. */int /* O - 1 on success, 0 on failure */SendHeader(client_t *con, /* I - Client to send to */ http_status_t code, /* I - HTTP status code */ char *type) /* I - MIME type of document */{#if 0 location_t *loc; /* Authentication location */#endif if (httpPrintf(HTTP(con), "HTTP/%d.%d %d %s\r\n", con->http.version / 100, con->http.version % 100, code, httpStatus(code)) < 0) return (0); if (httpPrintf(HTTP(con), "Date: %s\r\n", httpGetDateString(time(NULL))) < 0) return (0); if (ServerHeader) if (httpPrintf(HTTP(con), "Server: %s\r\n", ServerHeader) < 0) return (0); if (con->http.keep_alive && con->http.version >= HTTP_1_0) { if (httpPrintf(HTTP(con), "Connection: Keep-Alive\r\n") < 0) return (0); if (httpPrintf(HTTP(con), "Keep-Alive: timeout=%d\r\n", KeepAliveTimeout) < 0) return (0); }#if 0 if (code == HTTP_METHOD_NOT_ALLOWED) if (httpPrintf(HTTP(con), "Allow: GET, HEAD, OPTIONS, POST\r\n") < 0) return (0); if (code == HTTP_UNAUTHORIZED) { /* * This already succeeded in IsAuthorized... */ loc = FindBest(con->uri, con->http.state); if (loc->type != AUTH_DIGEST) { if (httpPrintf(HTTP(con), "WWW-Authenticate: Basic realm=\"CUPS\"\r\n") < 0) return (0); } else { if (httpPrintf(HTTP(con), "WWW-Authenticate: Digest realm=\"CUPS\", " "nonce=\"%s\"\r\n", con->http.hostname) < 0) return (0); } }#endif if (con->language != NULL) { if (httpPrintf(HTTP(con), "Content-Language: %s\r\n", con->language->language) < 0) return (0); if (type != NULL) if (httpPrintf(HTTP(con), "Content-Type: %s; charset=%s\r\n", type, cupsLangEncoding(con->language)) < 0) return (0); } else if (type != NULL) if (httpPrintf(HTTP(con), "Content-Type: %s\r\n", type) < 0) return (0); return (1);}#if 0/* * 'UpdateCGI()' - Read status messages from CGI scripts and programs. */voidUpdateCGI(void){ int bytes; /* Number of bytes read */ char *lineptr, /* Pointer to end of line in buffer */ *message; /* Pointer to message text */ int loglevel; /* Log level for message */ static int bufused = 0; /* Number of bytes used in buffer */ static char buffer[1024]; /* Status buffer */ if ((bytes = read(CGIPipes[0], buffer + bufused, sizeof(buffer) - bufused - 1)) > 0) { bufused += bytes; buffer[bufused] = '\0'; lineptr = strchr(buffer, '\n'); } else if (bytes < 0 && errno == EINTR) return; else { lineptr = buffer + bufused; lineptr[1] = 0; } if (bytes == 0 && bufused == 0) lineptr = NULL; while (lineptr != NULL) { /* * Terminate each line and process it... */ *lineptr++ = '\0'; /* * Figure out the logging level... */ if (strncmp(buffer, "EMERG:", 6) == 0) { loglevel = L_EMERG; message = buffer + 6; } else if (strncmp(buffer, "ALERT:", 6) == 0) { loglevel = L_ALERT; message = buffer + 6; } else if (strncmp(buffer, "CRIT:", 5) == 0) { loglevel = L_CRIT; message = buffer + 5; } else if (strncmp(buffer, "ERROR:", 6) == 0) { loglevel = L_ERROR; message = buffer + 6; } else if (strncmp(buffer, "WARNING:", 8) == 0) { loglevel = L_WARN; message = buffer + 8; } else if (strncmp(buffer, "NOTICE:", 6) == 0) { loglevel = L_NOTICE; message = buffer + 6; } else if (strncmp(buffer, "INFO:", 5) == 0) { loglevel = L_INFO; message = buffer + 5; } else if (strncmp(buffer, "DEBUG:", 6) == 0) { loglevel = L_DEBUG; message = buffer + 6; } else if (strncmp(buffer, "DEBUG2:", 7) == 0) { loglevel = L_DEBUG2; message = buffer + 7; } else if (strncmp(buffer, "PAGE:", 5) == 0) { loglevel = L_PAGE; message = buffer + 5; } else { loglevel = L_DEBUG; message = buffer; } /* * Skip leading whitespace in the message... */ while (isspace(*message)) message ++; LogMessage(loglevel, "[CGI] %s", message); /* * Copy over the buffer data we've used up... */ strcpy(buffer, lineptr); bufused -= lineptr - buffer; if (bufused < 0) bufused = 0; lineptr = strchr(buffer, '\n'); } if (bytes <= 0) { /* * Fatal error on pipe - should never happen! */ LogMessage(L_ERROR, "UpdateCGI: error reading from CGI error pipe - %s", strerror(errno)); }}#endif/* * 'WriteClient()' - Write data to a client as needed. */int /* O - 1 if success, 0 if fail */WriteClient(client_t *con) /* I - Client connection */{ int bytes; /* Number of bytes written */ ipp_state_t ipp_state; /* IPP state value */#if 0 char buf[HTTP_MAX_BUFFER + 1];/* Data buffer */ char *bufptr; /* Pointer into buffer */#endif#ifdef DEBUG LogMessage(L_DEBUG2, "WriteClient(con=%p) %d response=%p, file=%d pipe_pid=%d", con, con->http.fd, con->response, con->file, con->pipe_pid);#endif /* DEBUG */ DEBUG_printf(("\nFunction WriteClient() START...... \n")); if (con->http.state != HTTP_GET_SEND && con->http.state != HTTP_POST_SEND) return (1); if (con->response != NULL) { ipp_state = ippWrite(&(con->http), con->response); bytes = ipp_state != IPP_ERROR && ipp_state != IPP_DATA; }#if 0 else if ((bytes = read(con->file, buf, HTTP_MAX_BUFFER)) > 0) {#ifdef DEBUG LogMessage(L_DEBUG2, "WriteClient: Read %d bytes from file %d...", bytes, con->file);#endif /* DEBUG */ if (con->pipe_pid && !con->got_fields) { /* * Inspect the data for Content-Type and other fields. */ buf[bytes] = '\0'; for (bufptr = buf; !con->got_fields && *bufptr; bufptr ++) if (*bufptr == '\n') { /* * Send line to client... */ if (bufptr > buf && bufptr[-1] == '\r') bufptr[-1] = '\0'; *bufptr++ = '\0'; httpPrintf(HTTP(con), "%s\r\n", buf); LogMessage(L_DEBUG2, "WriteClient: %d %s", con->http.fd, buf); /* * Update buffer... */ bytes -= (bufptr - buf); memmove(buf, bufptr, bytes + 1); bufptr = buf - 1; /* * See if the line was empty... */ if (con->field_col == 0) con->got_fields = 1; else con->field_col = 0; } else if (*bufptr != '\r') con->field_col ++; if (bytes > 0 && !con->got_fields) { /* * Remaining text needs to go out... */ httpPrintf(HTTP(con), "%s", buf); con->http.activity = time(NULL); return (1); } else if (bytes == 0) { con->http.activity = time(NULL); return (1); } } if (httpWrite(HTTP(con), buf, bytes) < 0) { CloseClient(con); return (0); } con->bytes += bytes; }#endif if (bytes <= 0) { LogRequest(con, HTTP_OK); if (con->http.data_encoding == HTTP_ENCODE_CHUNKED) { if (httpPrintf(HTTP(con), "0\r\n\r\n") < 0) { CloseClient(con); return (0); } } con->http.state = HTTP_WAITING; LogMessage(L_DEBUG2, "WriteClient: Removing fd %d from OutputSet...", con->http.fd); FD_CLR(con->http.fd, OutputSet); if (con->file >= 0) { if (FD_ISSET(con->file, InputSet)) { LogMessage(L_DEBUG2, "WriteClient: Removing fd %d from InputSet...", con->file); FD_CLR(con->file, InputSet); } if (con->pipe_pid) kill(con->pipe_pid, SIGTERM); LogMessage(L_DEBUG2, "WriteClient: %d Closing data file %d.", con->http.fd, con->file); close(con->file); con->file = -1; SFD = -1; con->pipe_pid = 0; } if (con->filename) { LogMessage(L_DEBUG2, "WriteClient: %d Removing temp file %s", con->http.fd, con->filename); #if 0 unlink(con->filename);#endif ClearString(&con->filename); } if (con->request != NULL) { ippDelete(con->request); con->request = NULL; } if (con->response != NULL) { ippDelete(con->response); con->response = NULL; } ClearString(&con->command); ClearString(&con->options); if (!con->http.keep_alive) { CloseClient(con); return (0); } } else { con->file_ready = 0; if (con->pipe_pid && !FD_ISSET(con->file, InputSet)) { LogMessage(L_DEBUG2, "WriteClient: Adding fd %d to InputSet...", con->file); FD_SET(con->file, InputSet); } } if (bytes >= 1024) LogMessage(L_DEBUG2, "WriteClient: %d %d bytes", con->http.fd, bytes); con->http.activity = time(NULL); DEBUG_printf(("Function WriteClient()\n\n")); return (1);}#if 0/* * 'check_if_modified()' - Decode an "If-Modified-Since" line. */static int /* O - 1 if modified since */check_if_modified(client_t *con, /* I - Client connection */ struct stat *filestats) /* I - File information */{ char *ptr; /* Pointer into field */ time_t date; /* Time/date value */ int size; /* Size/length value */ size = 0; date = 0; ptr = con->http.fields[HTTP_FIELD_IF_MODIFIED_SINCE]; if (*ptr == '\0') return (1); LogMessage(L_DEBUG2, "check_if_modified: %d If-Modified-Since=\"%s\"", con->http.fd, ptr); while (*ptr != '\0') { while (isspace(*ptr) || *ptr == ';') ptr ++; if (strncasecmp(ptr, "length=", 7) == 0) { ptr += 7; size = atoi(ptr); while (isdigit(*ptr)) ptr ++; } else if (isalpha(*ptr)) { date = httpGetDateTime(ptr); while (*ptr != '\0' && *ptr != ';') ptr ++; } } LogMessage(L_DEBUG2, "check_if_modified: %d sizes=%d,%d dates=%d,%d", con->http.fd, size, (int)filestats->st_size, (int)date, (int)filestats->st_mtime); return ((size != filestats->st_size && size != 0) || (date < filestats->st_mtime && date != 0) || (size == 0 && date == 0));}/* * 'decode_auth()' - Decode an authorization string. */static voiddecode_auth(client_t *con) /* I - Client to decode to */{ char *s, /* Authorization string */ value[1024]; /* Value string */ const char *username; /* Certificate username */ /* * Decode the string... */ s = con->http.fields[HTTP_FIELD_AUTHORIZATION]; LogMessage(L_DEBUG2, "decode_auth(%p): Authorization string = \"%s\"", con, s); if (strncmp(s, "Basic", 5) == 0) { s += 5; while (isspace(*s)) s ++; httpDecode64(value, s); /* * Pull the u
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -