Beispiel #1
0
request::form_type const &request::post_or_get()
{
	if(request_method()=="POST")
		return post_;
	else
		return get_;
}
Beispiel #2
0
    // Error-code semantics
    boost::system::error_code
      load(parse_options parse_opts, boost::system::error_code& ec
          , char** base_environment = NULL, bool is_command_line = true)
    {
      // Parse just the environment first, then check the user
      // isn't trying to upload more data than we want to let them.
      // Define `BOOST_CGI_POST_MAX` to set the maximum content-length
      // allowed.
      if (parse_opts & parse_env)
      {
        //this->service.load(this->implementation, parse_env, ec);
        //if (content_length() >= BOOST_CGI_POST_MAX)
        //  ec = common::error::max_post_exceeded;
        this->service.load(this->implementation, parse_opts, ec);
        
        if (ec) return ec;

        // Load the environment passed by the user.
        if (base_environment)
          this->service.load_environment(
              this->implementation, base_environment
            , is_command_line);
        
        if (parse_opts & parse_env)
          env.set(env_vars(this->implementation.vars_));
        if (parse_opts & parse_get_only)
          get.set(get_vars(this->implementation.vars_));
        if (parse_opts & parse_post_only) {
          post.set(post_vars(this->implementation.vars_));
          uploads.set(upload_vars(this->implementation.vars_));
        }
        if ((parse_opts & parse_cookies) == parse_cookies) {
          cookies.set(cookie_vars(this->implementation.vars_));
        }
        if ((parse_opts & parse_form_only) == parse_form_only)
        {
          common::name rm(request_method().c_str());
          form.set(rm == "POST" ? post.impl() : get.impl());
        }
#ifdef BOOST_CGI_ENABLE_SESSIONS
        if (parse_opts & parse_session_only)
        {
          if (!!cookies && cookies.count(BOOST_CGI_SESSION_COOKIE_NAME))
            session.id(cookies[BOOST_CGI_SESSION_COOKIE_NAME]);
          else
          if (traits::auto_start_session)
            session.id(this->service.make_session_id());
          if (!session.id().empty())
            this->service.session_manager().load(session);
        }
#endif // BOOST_CGI_ENABLE_SESSIONS
      }
      return ec;
    }
Beispiel #3
0
static void prepare_debug(void)
{
    /* Debug preparation */
#if CGI_DEBUG == CGI_DEBUG_BYQUERY
    if (getenv("QUERY_STRING") != NULL && strcmp(getenv("QUERY_STRING"), "CGI_DEBUG=ON") == 0) {
	/* Debug enabling */
	    printf("Content-Type: text/html\n");
	    printf("Set-Cookie: _CGI_DEBUG_ON_=1; Version=1\n\n");
	    printf("<html><head></head><body>\n");
	    printf("<h2 align=\"center\"><font color=\"red\">CGI DEBUG is enabled</font></h2>\n");
	    printf("</body></html>\n");
	    exit(0);
    } else if (getenv("QUERY_STRING") != NULL && strcmp(getenv("QUERY_STRING"), "CGI_DEBUG=OFF") == 0) {
	/* Debug disabling */
	    printf("Content-Type: text/html\n");
	    printf("Set-Cookie: _CGI_DEBUG_ON_=1; Version=1; Max-Age=0\n\n");
	    printf("<html><head></head><body>\n");
	    printf("<h2 align=\"center\"><font color=\"green\">CGI DEBUG is disabled</font></h2>\n");
	    printf("</body></html>\n");
	    exit(0);
    } else if (getenv("HTTP_COOKIE") != NULL && strstr(getenv("HTTP_COOKIE"), "_CGI_DEBUG_ON_") != NULL) {
#else
    {
#endif
	char inpath[64], envpath[64], outpath[64], errpath[64];
	char buf[256];
	char **envp;
	int inTextLength = 0;
	if (getenv("QUERY_STRING") != NULL && strncmp(getenv("QUERY_STRING"), "CGI_OUT_", 8) == 0) {
	    /* Send previous CGI response (if exists) */
	    FILE *in = fopen(getenv("QUERY_STRING"), "r");
	    if (in != NULL) {
		fclose(in);
		sprintf(buf, "cat %s", getenv("QUERY_STRING"));
		system(buf);
		system("rm CGI_*_*.log");
		exit(0);
	    }
	}
	if (getenv("CONTENT_LENGTH") != NULL && getenv("CONTENT_TYPE") != NULL &&
	    (strcmp(getenv("CONTENT_TYPE"), MIME_URLFORM) == 0 || strncmp(getenv("CONTENT_TYPE"), "text/", 5) == 0))
	{
	    inTextLength = atoi(getenv("CONTENT_LENGTH"));
	}
	/* Redirect CGI outputs & sends info page */
	    sprintf(envpath, "CGI_ENV_%d.log", getpid());
	    sprintf(outpath, "CGI_OUT_%d.log", getpid());
	    sprintf(errpath, "CGI_ERR_%d.log", getpid());
	    printf("Content-Type: text/html\n\n");
	    printf("<html><head></head><body>\n");
	    printf("<p><a href='%s'>Environment variables:</a></p>\n<table>", envpath);
	    for (envp = environ; *envp != NULL; envp++) {
		char *eq = strchr(*envp, '=');
		if (eq != NULL) {
		    *eq = 0;
		    printf("<tr><th align=\"right\">"); print_esc_env(*envp);
		    printf("</th><td align=\"left\">"); print_esc_env(eq + 1); printf("</td></tr>\n");
		    *eq = '=';
		}
	    }
	    printf("</table>\n");
	    if (inTextLength > 0) {
		sprintf(inpath, "CGI_IN_%d.log", getpid());
		printf("<p><a href='%s'>CGI input</a></p>\n", inpath);
	    }
	    printf("<p><a href='%s'>CGI output</a></p>\n", outpath);
	    printf("<p><a href='%s'>CGI errors</a></p>\n", errpath);
	    printf("<p><a href='%s?%s'>Send response to browser (and remove logs)</a></p>\n", getenv("SCRIPT_NAME"), outpath);
	    printf("<p><a href='%s?CGI_DEBUG=OFF'><font color=\"green\">Disable debug</font></a></p>\n", getenv("SCRIPT_NAME"));
	    printf("</body></html>\n");
	    fflush(stdout);
	    sprintf(buf, "/usr/bin/env >%s", envpath);
	    system(buf);
	    if (inTextLength > 0) {
		char *input = read_std_file(stdin, inTextLength);
		FILE *finput = fopen(inpath, "w");
		fprintf(finput, "%s", input);
		fclose(finput);
		free(input);
		freopen(inpath, "r", stdin);
	    }
	    //***outcopyfd = dup(1);
	    freopen(outpath, "w", stdout);
	    freopen(errpath, "w", stderr);
	    setbuf(stdout,NULL);
    }
}
#endif


void cgiMain(void);

int main(int argc, char **argv)
{
#if CGI_DEBUG > CGI_DEBUG_NEVER
    prepare_debug();
#endif

    /* Request processing */
    if (getenv("QUERY_STRING") != NULL) {
	qs_form = Form_create(getenv("QUERY_STRING"));
    }
    if (strcmp(request_method(), "POST") == 0 && request_contentType() != NULL &&
	strcmp(request_contentType(), MIME_URLFORM) == 0)
    {
	    char *form_s = read_std_file(stdin, request_contentLength());
	    content_form = Form_create(form_s);
	    free(form_s);
    }

    /* Session-id Cookie processing */
/***
    char *cookie = getenv("HTTP_COOKIE");
    if (cookie == NULL) {
	session_id = NULL;
    } else {
	size_t pos1 = strcspn(cookie, "=");
	if (strncmp(cookie, CGI_SESSIONID_NAME, pos1) == 0) {
	    size_t pos2 = strcspn(cookie+pos1+1, ";");
	    session_id = (char*) malloc(pos2 + 1);
	    strncpy(session_id, cookie+pos1+1, pos2);
	} else {
	    session_id = NULL;
	}
    }
request_cookieCount();
***/
    cgiMain();

    return 0;
}