Example #1
0
/*
 * Setup environment variables as per the CGI spec
 */
void setenv_cgi(state *st, char *script)
{
	char buf[BUFSIZE];

	/* Security */
	setenv("PATH", SAFE_PATH, 1);

	/* Set up the environment as per CGI spec */
	setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
	setenv("CONTENT_LENGTH", "0", 1);
	setenv("QUERY_STRING", st->req_query_string, 1);
	snprintf(buf, sizeof(buf), SERVER_SOFTWARE_FULL, st->server_platform);
	setenv("SERVER_SOFTWARE", buf, 1);
	setenv("SERVER_ARCH", st->server_platform, 1);
	setenv("SERVER_DESCRIPTION", st->server_description, 1);
	snprintf(buf, sizeof(buf), SERVER_SOFTWARE "/" VERSION);
	setenv("SERVER_VERSION", buf, 1);

	if (st->req_protocol == PROTO_HTTP)
		setenv("SERVER_PROTOCOL", "HTTP/0.9", 1);
	else
		setenv("SERVER_PROTOCOL", "RFC1436", 1);

	setenv("SERVER_NAME", st->server_host, 1);
	snprintf(buf, sizeof(buf), "%i", st->server_port);
	setenv("SERVER_PORT", buf, 1);
	setenv("REQUEST_METHOD", "GET", 1);
	setenv("DOCUMENT_ROOT", st->server_root, 1);
	setenv("SCRIPT_NAME", st->req_selector, 1);
	setenv("SCRIPT_FILENAME", script, 1);
	setenv("LOCAL_ADDR", st->req_local_addr, 1);
	setenv("REMOTE_ADDR", st->req_remote_addr, 1);
	setenv("HTTP_REFERER", st->req_referrer, 1);
#ifdef HAVE_SHMEM
	snprintf(buf, sizeof(buf), "%x", st->session_id);
	setenv("SESSION_ID", buf, 1);
#endif
	setenv("HTTP_ACCEPT_CHARSET", strcharset(st->out_charset), 1);

	/* Gophernicus extras */
	snprintf(buf, sizeof(buf), "%c", st->req_filetype);
	setenv("GOPHER_FILETYPE", buf, 1);
	setenv("GOPHER_CHARSET", strcharset(st->out_charset), 1);
	setenv("GOPHER_REFERER", st->req_referrer, 1);
	snprintf(buf, sizeof(buf), "%i", st->out_width);
	setenv("COLUMNS", buf, 1);

	/* Bucktooth extras */
	if (*st->req_query_string) {
		snprintf(buf, sizeof(buf), "%s?%s",
			st->req_selector, st->req_query_string);
		setenv("SELECTOR", buf, 1);
	}
	else setenv("SELECTOR", st->req_selector, 1);

	setenv("SERVER_HOST", st->server_host, 1);
	setenv("REQUEST", st->req_selector, 1);
	setenv("SEARCHREQUEST", st->req_query_string, 1);
}
Example #2
0
/// get page from network
bool getPage(const char* url, int method, std::string& response)
{
	// Todo : add proxy and authentication support
	response.clear();
	string strurl = url;
	boost::regex regspace(" ");
	strurl = boost::regex_replace(strurl, regspace, "%20");
	HttpWrap *phttpwrap = new HttpWrap();
	response += phttpwrap->Get(strurl.c_str());
	delete phttpwrap;
    phttpwrap = NULL;
	if (response.length() < 200)
	{
		return false;
	} 
	else
	{
		boost::regex regRn("\\r\\n");
		response = boost::regex_replace(response, regRn, "\\n");
		boost::regex reg1("<meta(.*?)charset=(.*?)>",boost::regex::icase); 
		boost::smatch what;
		std::string::const_iterator start = response.begin();
		std::string::const_iterator end1 = response.end();
		if( boost::regex_search(start, end1, what, reg1) )
		{
			string strcharset(what[2].first,what[2].second);
			if((int)strcharset.length() == 0)
			{
				if(!IsUTF8(response.c_str(),response.length()))
				{
					string strtmp = "";
					iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1);
					response = strtmp;

				}
			}
			else 
			{
				if ((int)strcharset.find("UTF") >= 0 || (int)strcharset.find("utf") >= 0)
				{
					;
				} 
				else
				{
					string strtmp = "";
					iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1);
					response = strtmp;
				}
			}
		}
		else if(!IsUTF8(response.c_str(),response.length())/*pSpiderconf->m_config.ruleCollections[pSpiderconf->m_config.plans[m_plan_id].collectionRuleId].charset != "utf-8"*/)
		{
			string strtmp = "";
			iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1);
			response = strtmp;

		}
	}
	return true;
}
Example #3
0
/*
 * Handle /caps.txt
 */
void caps_txt(state *st, shm_state *shm)
{
	/* Log the request */
	if (st->opt_syslog) {
		syslog(LOG_INFO, "request for \"gopher://%s:%i/0" CAPS_TXT "\" from %s",
			st->server_host,
			st->server_port,
			st->req_remote_addr);
	}
	log_combined(st, HTTP_OK);

	/* Update counters */
#ifdef HAVE_SHMEM
	if (shm) {
		shm->hits++;
		shm->kbytes += 1;

		/* Update session data */
		st->req_filesize += 1024;
		update_shm_session(st, shm);
	}
#endif

	/* Standard caps.txt stuff */
	printf("CAPS" CRLF
		CRLF
		"##" CRLF
		"## This is an automatically generated caps file." CRLF
		"##" CRLF
		CRLF
		"CapsVersion=1" CRLF
		"ExpireCapsAfter=%i" CRLF
		CRLF
		"PathDelimeter=/" CRLF
		"PathIdentity=." CRLF
		"PathParent=.." CRLF
		"PathParentDouble=FALSE" CRLF
		"PathKeepPreDelimeter=FALSE" CRLF
		"ServerSupportsStdinScripts=TRUE" CRLF
		"ServerDefaultEncoding=%s" CRLF
		CRLF
		"ServerSoftware=" SERVER_SOFTWARE CRLF
		"ServerSoftwareVersion=" VERSION CRLF
		"ServerArchitecture=%s" CRLF,
			st->session_timeout,
			strcharset(st->out_charset),
			st->server_platform);

	/* Optional keys */
	if (*st->server_description)
		printf("ServerDescription=%s" CRLF, st->server_description);
	if (*st->server_location)
		printf("ServerGeolocationString=%s" CRLF, st->server_location);
	if (*st->server_admin)
		printf("ServerAdmin=%s" CRLF, st->server_admin);
}