⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_cgi.c

📁 linux网络服务器工具
💻 C
📖 第 1 页 / 共 3 页
字号:
        for (bucket = APR_BRIGADE_FIRST(bb);             bucket != APR_BRIGADE_SENTINEL(bb);             bucket = APR_BUCKET_NEXT(bucket))        {            const char *data;            apr_size_t len;            if (APR_BUCKET_IS_EOS(bucket)) {                seen_eos = 1;                break;            }            /* We can't do much with this. */            if (APR_BUCKET_IS_FLUSH(bucket)) {                continue;            }            /* If the child stopped, we still must read to EOS. */            if (child_stopped_reading) {                continue;            }            /* read */            apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);            if (conf->logname && dbpos < conf->bufbytes) {                int cursize;                if ((dbpos + len) > conf->bufbytes) {                    cursize = conf->bufbytes - dbpos;                }                else {                    cursize = len;                }                memcpy(dbuf + dbpos, data, cursize);                dbpos += cursize;            }            /* Keep writing data to the child until done or too much time             * elapses with no progress or an error occurs.             */            rv = apr_file_write_full(script_out, data, len, NULL);            if (rv != APR_SUCCESS) {                /* silly script stopped reading, soak up remaining message */                child_stopped_reading = 1;            }        }        apr_brigade_cleanup(bb);    }    while (!seen_eos);    if (conf->logname) {        dbuf[dbpos] = '\0';    }    /* Is this flush really needed? */    apr_file_flush(script_out);    apr_file_close(script_out);    AP_DEBUG_ASSERT(script_in != NULL);    apr_brigade_cleanup(bb);#if APR_FILES_AS_SOCKETS    apr_file_pipe_timeout_set(script_in, 0);    apr_file_pipe_timeout_set(script_err, 0);    b = cgi_bucket_create(r, script_in, script_err, c->bucket_alloc);#else    b = apr_bucket_pipe_create(script_in, c->bucket_alloc);#endif    APR_BRIGADE_INSERT_TAIL(bb, b);    b = apr_bucket_eos_create(c->bucket_alloc);    APR_BRIGADE_INSERT_TAIL(bb, b);    /* Handle script return... */    if (!nph) {        const char *location;        char sbuf[MAX_STRING_LEN];        int ret;        if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) {            ret = log_script(r, conf, ret, dbuf, sbuf, bb, script_err);            /*             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script             * does not set an explicit status and ap_meets_conditions, which             * is called by ap_scan_script_header_err_brigade, detects that             * the conditions of the requests are met and the response is             * not modified.             * In this case set r->status and return OK in order to prevent             * running through the error processing stack as this would             * break with mod_cache, if the conditions had been set by             * mod_cache itself to validate a stale entity.             * BTW: We circumvent the error processing stack anyway if the             * CGI script set an explicit status code (whatever it is) and             * the only possible values for ret here are:             *             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the             * processing of the response of the CGI script, e.g broken headers             * or a crashed CGI process).             */            if (ret == HTTP_NOT_MODIFIED) {                r->status = ret;                return OK;            }            return ret;        }        location = apr_table_get(r->headers_out, "Location");        if (location && r->status == 200) {            /* For a redirect whether internal or not, discard any             * remaining stdout from the script, and log any remaining             * stderr output, as normal. */            discard_script_output(bb);            apr_brigade_destroy(bb);            apr_file_pipe_timeout_set(script_err, r->server->timeout);            log_script_err(r, script_err);        }        if (location && location[0] == '/' && r->status == 200) {            /* This redirect needs to be a GET no matter what the original             * method was.             */            r->method = apr_pstrdup(r->pool, "GET");            r->method_number = M_GET;            /* We already read the message body (if any), so don't allow             * the redirected request to think it has one.  We can ignore             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.             */            apr_table_unset(r->headers_in, "Content-Length");            ap_internal_redirect_handler(location, r);            return OK;        }        else if (location && r->status == 200) {            /* XX Note that if a script wants to produce its own Redirect             * body, it now has to explicitly *say* "Status: 302"             */            return HTTP_MOVED_TEMPORARILY;        }        rv = ap_pass_brigade(r->output_filters, bb);    }    else /* nph */ {        struct ap_filter_t *cur;        /* get rid of all filters up through protocol...  since we         * haven't parsed off the headers, there is no way they can         * work         */        cur = r->proto_output_filters;        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {            cur = cur->next;        }        r->output_filters = r->proto_output_filters = cur;        rv = ap_pass_brigade(r->output_filters, bb);    }    /* don't soak up script output if errors occurred writing it     * out...  otherwise, we prolong the life of the script when the     * connection drops or we stopped sending output for some other     * reason */    if (rv == APR_SUCCESS && !r->connection->aborted) {        apr_file_pipe_timeout_set(script_err, r->server->timeout);        log_script_err(r, script_err);    }    apr_file_close(script_err);    return OK;                      /* NOT r->status, even if it has changed. */}/*============================================================================ *============================================================================ * This is the beginning of the cgi filter code moved from mod_include. This *   is the code required to handle the "exec" SSI directive. *============================================================================ *============================================================================*/static apr_status_t include_cgi(include_ctx_t *ctx, ap_filter_t *f,                                apr_bucket_brigade *bb, char *s){    request_rec *r = f->r;    request_rec *rr = ap_sub_req_lookup_uri(s, r, f->next);    int rr_status;    if (rr->status != HTTP_OK) {        ap_destroy_sub_req(rr);        return APR_EGENERAL;    }    /* No hardwired path info or query allowed */    if ((rr->path_info && rr->path_info[0]) || rr->args) {        ap_destroy_sub_req(rr);        return APR_EGENERAL;    }    if (rr->finfo.filetype != APR_REG) {        ap_destroy_sub_req(rr);        return APR_EGENERAL;    }    /* Script gets parameters of the *document*, for back compatibility */    rr->path_info = r->path_info;       /* hard to get right; see mod_cgi.c */    rr->args = r->args;    /* Force sub_req to be treated as a CGI request, even if ordinary     * typing rules would have called it something else.     */    ap_set_content_type(rr, CGI_MAGIC_TYPE);    /* Run it. */    rr_status = ap_run_sub_req(rr);    if (ap_is_HTTP_REDIRECT(rr_status)) {        const char *location = apr_table_get(rr->headers_out, "Location");        if (location) {            char *buffer;            location = ap_escape_html(rr->pool, location);            buffer = apr_pstrcat(ctx->pool, "<a href=\"", location, "\">",                                 location, "</a>", NULL);            APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(buffer,                                    strlen(buffer), ctx->pool,                                    f->c->bucket_alloc));        }    }    ap_destroy_sub_req(rr);    return APR_SUCCESS;}static apr_status_t include_cmd(include_ctx_t *ctx, ap_filter_t *f,                                apr_bucket_brigade *bb, const char *command){    cgi_exec_info_t  e_info;    const char **argv;    apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL;    apr_status_t rv;    request_rec *r = f->r;    add_ssi_vars(r);    e_info.process_cgi = 0;    e_info.cmd_type    = APR_SHELLCMD;    e_info.detached    = 0;    e_info.in_pipe     = APR_NO_PIPE;    e_info.out_pipe    = APR_FULL_BLOCK;    e_info.err_pipe    = APR_NO_PIPE;    e_info.prog_type   = RUN_AS_SSI;    e_info.bb          = &bb;    e_info.ctx         = ctx;    e_info.next        = f->next;    e_info.addrspace   = 0;    if ((rv = cgi_build_command(&command, &argv, r, r->pool,                                &e_info)) != APR_SUCCESS) {        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,                      "don't know how to spawn cmd child process: %s",                      r->filename);        return rv;    }    /* run the script in its own process */    if ((rv = run_cgi_child(&script_out, &script_in, &script_err,                            command, argv, r, r->pool,                            &e_info)) != APR_SUCCESS) {        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,                      "couldn't spawn child process: %s", r->filename);        return rv;    }    APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pipe_create(script_in,                            f->c->bucket_alloc));    ctx->flush_now = 1;    /* We can't close the pipe here, because we may return before the     * full CGI has been sent to the network.  That's okay though,     * because we can rely on the pool to close the pipe for us.     */    return APR_SUCCESS;}static apr_status_t handle_exec(include_ctx_t *ctx, ap_filter_t *f,                                apr_bucket_brigade *bb){    char *tag = NULL;    char *tag_val = NULL;    request_rec *r = f->r;    char *file = r->filename;    char parsed_string[MAX_STRING_LEN];    if (!ctx->argc) {        ap_log_rerror(APLOG_MARK,                      (ctx->flags & SSI_FLAG_PRINTING)                          ? APLOG_ERR : APLOG_WARNING,                      0, r, "missing argument for exec element in %s",                      r->filename);    }    if (!(ctx->flags & SSI_FLAG_PRINTING)) {        return APR_SUCCESS;    }    if (!ctx->argc) {        SSI_CREATE_ERROR_BUCKET(ctx, f, bb);        return APR_SUCCESS;    }    if (ctx->flags & SSI_FLAG_NO_EXEC) {        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "exec used but not allowed "                      "in %s", r->filename);        SSI_CREATE_ERROR_BUCKET(ctx, f, bb);        return APR_SUCCESS;    }    while (1) {        cgi_pfn_gtv(ctx, &tag, &tag_val, SSI_VALUE_DECODED);        if (!tag || !tag_val) {            break;        }        if (!strcmp(tag, "cmd")) {            apr_status_t rv;            cgi_pfn_ps(ctx, tag_val, parsed_string, sizeof(parsed_string),                       SSI_EXPAND_LEAVE_NAME);            rv = include_cmd(ctx, f, bb, parsed_string);            if (rv != APR_SUCCESS) {                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "execution failure "                              "for parameter \"%s\" to tag exec in file %s",                              tag, r->filename);                SSI_CREATE_ERROR_BUCKET(ctx, f, bb);                break;            }        }        else if (!strcmp(tag, "cgi")) {            apr_status_t rv;            cgi_pfn_ps(ctx, tag_val, parsed_string, sizeof(parsed_string),                       SSI_EXPAND_DROP_NAME);            rv = include_cgi(ctx, f, bb, parsed_string);            if (rv != APR_SUCCESS) {                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "invalid CGI ref "                              "\"%s\" in %s", tag_val, file);                SSI_CREATE_ERROR_BUCKET(ctx, f, bb);                break;            }        }        else {            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "unknown parameter "                          "\"%s\" to tag exec in %s", tag, file);            SSI_CREATE_ERROR_BUCKET(ctx, f, bb);            break;        }    }    return APR_SUCCESS;}/*============================================================================ *============================================================================ * This is the end of the cgi filter code moved from mod_include. *============================================================================ *============================================================================*/static int cgi_post_config(apr_pool_t *p, apr_pool_t *plog,                                apr_pool_t *ptemp, server_rec *s){    cgi_pfn_reg_with_ssi = APR_RETRIEVE_OPTIONAL_FN(ap_register_include_handler);    cgi_pfn_gtv          = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_get_tag_and_value);    cgi_pfn_ps           = APR_RETRIEVE_OPTIONAL_FN(ap_ssi_parse_string);    if ((cgi_pfn_reg_with_ssi) && (cgi_pfn_gtv) && (cgi_pfn_ps)) {        /* Required by mod_include filter. This is how mod_cgi registers         *   with mod_include to provide processing of the exec directive.         */        cgi_pfn_reg_with_ssi("exec", handle_exec);    }    /* This is the means by which unusual (non-unix) os's may find alternate     * means to run a given command (e.g. shebang/registry parsing on Win32)     */    cgi_build_command    = APR_RETRIEVE_OPTIONAL_FN(ap_cgi_build_command);    if (!cgi_build_command) {        cgi_build_command = default_build_command;    }    return OK;}static void register_hooks(apr_pool_t *p){    static const char * const aszPre[] = { "mod_include.c", NULL };    ap_hook_handler(cgi_handler, NULL, NULL, APR_HOOK_MIDDLE);    ap_hook_post_config(cgi_post_config, aszPre, NULL, APR_HOOK_REALLY_FIRST);}module AP_MODULE_DECLARE_DATA cgi_module ={    STANDARD20_MODULE_STUFF,    NULL,                        /* dir config creater */    NULL,                        /* dir merger --- default is to override */    create_cgi_config,           /* server config */    merge_cgi_config,            /* merge server config */    cgi_cmds,                    /* command apr_table_t */    register_hooks               /* register hooks */};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -