📄 proxy_ftp.c
字号:
if (len == 0) { get_dirlisting = 1; } else { ap_bvputs(ctrl, "SIZE ", path, CRLF, NULL); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: SIZE %s", path); i = ftp_getrc_msg(ctrl, resp, sizeof resp); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d with response %s", i, resp); if (i != 500) { /* Size command not recognized */ if (i == 550) { /* Not a regular file */ ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: SIZE shows this is a directory"); get_dirlisting = 1; ap_bvputs(ctrl, "CWD ", path, CRLF, NULL); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: CWD %s", path); /* possible results: 250, 421, 500, 501, 502, 530, 550 */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 530 Not logged in. */ /* 550 Requested action not taken. */ i = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d", i); if (i == -1 || i == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); if (i == 550) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_NOT_FOUND); if (i != 250) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_BAD_GATEWAY); path = ""; len = 0; } else if (i == 213) {/* Size command ok */ for (j = 0; j < sizeof resp && ap_isdigit(resp[j]); j++); resp[j] = '\0'; if (resp[0] != '\0') size = ap_pstrdup(p, resp); } } }#ifdef AUTODETECT_PWD ap_bvputs(ctrl, "PWD", CRLF, NULL); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: PWD");/* responses: 257, 500, 501, 502, 421, 550 */ /* 257 "<directory-name>" <commentary> */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ i = ftp_getrc_msg(ctrl, resp, sizeof resp); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: PWD returned status %d", i); if (i == -1 || i == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); if (i == 550) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_NOT_FOUND); if (i == 257) { const char *dirp = resp; cwd = ap_getword_conf(r->pool, &dirp); }#endif /* AUTODETECT_PWD */ if (get_dirlisting) { if (len != 0) ap_bvputs(ctrl, "LIST ", path, CRLF, NULL); else ap_bputs("LIST -lag" CRLF, ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: LIST %s", (len == 0 ? "" : path)); } else { ftp_set_TYPE(r, ctrl, xfer_type); ap_bvputs(ctrl, "RETR ", path, CRLF, NULL); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: RETR %s", path); } ap_bflush(ctrl);/* RETR: 110, 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 530, 550 NLST: 125, 150, 226, 250, 421, 425, 426, 450, 451, 500, 501, 502, 530 */ /* 110 Restart marker reply. */ /* 125 Data connection already open; transfer starting. */ /* 150 File status okay; about to open data connection. */ /* 226 Closing data connection. */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 425 Can't open data connection. */ /* 426 Connection closed; transfer aborted. */ /* 450 Requested file action not taken. */ /* 451 Requested action aborted. Local error in processing. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 530 Not logged in. */ /* 550 Requested action not taken. */ rc = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d", rc); if (rc == -1 || rc == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); if (rc == 550) { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: RETR failed, trying LIST instead"); get_dirlisting = 1; ftp_set_TYPE(r, ctrl, 'A'); /* directories must be transferred in * ASCII */ ap_bvputs(ctrl, "CWD ", path, CRLF, NULL); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: CWD %s", path); /* possible results: 250, 421, 500, 501, 502, 530, 550 */ /* 250 Requested file action okay, completed. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 530 Not logged in. */ /* 550 Requested action not taken. */ rc = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d", rc); if (rc == -1 || rc == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); if (rc == 550) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_NOT_FOUND); if (rc != 250) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_BAD_GATEWAY);#ifdef AUTODETECT_PWD ap_bvputs(ctrl, "PWD", CRLF, NULL); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: PWD");/* responses: 257, 500, 501, 502, 421, 550 */ /* 257 "<directory-name>" <commentary> */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ /* 550 Requested action not taken. */ i = ftp_getrc_msg(ctrl, resp, sizeof resp); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: PWD returned status %d", i); if (i == -1 || i == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); if (i == 550) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_NOT_FOUND); if (i == 257) { const char *dirp = resp; cwd = ap_getword_conf(r->pool, &dirp); }#endif /* AUTODETECT_PWD */ ap_bputs("LIST -lag" CRLF, ctrl); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: LIST -lag"); rc = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d", rc); if (rc == -1 || rc == 421) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, ap_proxyerror(r, HTTP_BAD_GATEWAY, "Error reading from remote server")); } ap_kill_timeout(r); if (rc != 125 && rc != 150 && rc != 226 && rc != 250) return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_BAD_GATEWAY); r->status = HTTP_OK; r->status_line = "200 OK"; resp_hdrs = ap_make_table(p, 2); c->hdrs = resp_hdrs; ap_table_setn(resp_hdrs, "Date", ap_gm_timestr_822(r->pool, r->request_time)); ap_table_setn(resp_hdrs, "Server", ap_get_server_version()); if (get_dirlisting) { ap_table_setn(resp_hdrs, "Content-Type", "text/html");#ifdef CHARSET_EBCDIC r->ebcdic.conv_out = 1; /* server-generated */#endif } else {#ifdef CHARSET_EBCDIC r->ebcdic.conv_out = 0; /* do not convert what we read from the ftp * server */#endif if (r->content_type != NULL) { ap_table_setn(resp_hdrs, "Content-Type", r->content_type); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: Content-Type set to %s", r->content_type); } else { ap_table_setn(resp_hdrs, "Content-Type", ap_default_type(r)); } if (xfer_type != 'A' && size != NULL) { /* We "trust" the ftp server to really serve (size) bytes... */ ap_table_set(resp_hdrs, "Content-Length", size); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: Content-Length set to %s", size); } } if (r->content_encoding != NULL && r->content_encoding[0] != '\0') { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: Content-Encoding set to %s", r->content_encoding); ap_table_setn(resp_hdrs, "Content-Encoding", r->content_encoding); }/* check if NoCache directive on this host */ if (nocache == 0) { for (i = 0; i < conf->nocaches->nelts; i++) { if (destaddr.s_addr == ncent[i].addr.s_addr || (ncent[i].name != NULL && (ncent[i].name[0] == '*' || strstr(desthost, ncent[i].name) != NULL))) { nocache = 1; break; } } } i = ap_proxy_cache_update(c, resp_hdrs, 0, nocache); if (i != DECLINED) { return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, i); } if (!pasvmode) { /* wait for connection */ ap_hard_timeout("proxy ftp data connect", r); clen = sizeof(struct sockaddr_in); do csd = accept(dsock, (struct sockaddr *)&server, &clen); while (csd == -1 && errno == EINTR); if (csd == -1) { ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "proxy: failed to accept data connection"); if (c != NULL) c = ap_proxy_cache_error(c); return ftp_cleanup_and_return(r, ctrl, data, sock, dsock, HTTP_BAD_GATEWAY); } data = ap_bcreate(p, B_RDWR | B_SOCKET); ap_bpushfd(data, csd, -1); ap_kill_timeout(r); } else { data = ap_bcreate(p, B_RDWR | B_SOCKET); ap_bpushfd(data, dsock, dsock); } ap_hard_timeout("proxy receive", r); /* send response */ /* write status line and headers to the cache file */ ap_proxy_write_headers(c, ap_pstrcat(p, "HTTP/1.1 ", r->status_line, NULL), resp_hdrs); /* Setup the headers for our client from upstreams response-headers */ ap_overlap_tables(r->headers_out, resp_hdrs, AP_OVERLAP_TABLES_SET); /* Add X-Cache header */ ap_table_setn(r->headers_out, "X-Cache", ap_pstrcat(r->pool, "MISS from ", ap_get_server_name(r), NULL)); /* The Content-Type of this response is the upstream one. */ r->content_type = ap_table_get(r->headers_out, "Content-Type"); /* finally output the headers to the client */ ap_send_http_header(r);#ifdef CHARSET_EBCDIC ap_bsetflag(r->connection->client, B_EBCDIC2ASCII, r->ebcdic.conv_out);#endif/* send body */ if (!r->header_only) { if (!get_dirlisting) {/* we need to set this for ap_proxy_send_fb()... */ if (c != NULL) c->cache_completion = 0; ap_proxy_send_fb(data, r, c, -1, 0, 0, conf->io_buffer_size); } else { send_dir(data, r, c, cwd); } /* ap_proxy_send_fb() closes the socket */ data = NULL; dsock = -1; /* * We checked for 125||150||226||250 above. See if another rc is * pending, and fetch it: */ if (rc == 125 || rc == 150) rc = ftp_getrc(ctrl); } else {/* abort the transfer: we send the header only */ ap_bputs("ABOR" CRLF, ctrl); ap_bflush(ctrl); if (data != NULL) { ap_bclose(data); data = NULL; dsock = -1; } ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: ABOR");/* responses: 225, 226, 421, 500, 501, 502 */ /* 225 Data connection open; no transfer in progress. */ /* 226 Closing data connection. */ /* 421 Service not available, closing control connection. */ /* 500 Syntax error, command unrecognized. */ /* 501 Syntax error in parameters or arguments. */ /* 502 Command not implemented. */ i = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: returned status %d", i); } ap_kill_timeout(r); ap_proxy_cache_tidy(c);/* finish */ ap_bputs("QUIT" CRLF, ctrl); ap_bflush(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: QUIT");/* responses: 221, 500 */ /* 221 Service closing control connection. */ /* 500 Syntax error, command unrecognized. */ i = ftp_getrc(ctrl); ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, r->server, "FTP: QUIT: status %d", i); ap_bclose(ctrl); ap_rflush(r); /* flush before garbage collection */ ap_proxy_garbage_coll(r); return OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -