Example #1
0
static bool create_http_servlet(file_tmpl& tmpl)
{
	tpl_t* tpl = tmpl.open_tpl("http_servlet.cpp");
	if (tpl == NULL)
		return false;

	printf("Do you want add cookie? y[y/n]: "); fflush(stdout);
	int ch = getchar();
	if (ch != 'n')
		set_cookies(tpl);

	string filepath;
	filepath.format("%s/http_servlet.cpp", tmpl.get_project_name());
	if (tpl_save_as(tpl, filepath.c_str()) != TPL_OK)
	{
		printf("save to %s error: %s\r\n", filepath.c_str(),
			last_serror());
		tpl_free(tpl);
		return false;
	}

	printf("create %s ok.\r\n", filepath.c_str());
	tpl_free(tpl);

	// 设置服务器模板类型
	return create_service(tmpl);
}
static void readconfig() {
	const char	*val = NULL;
#ifdef DEBUG
	fprintf(stderr, "Reading configuration file...");
#endif

	config_t cfg;

	config_init(&cfg);
	if (!config_read_file(&cfg, config_file))   {
		char *message = "Config file not found in /etc/libnss.conf.\n";
		syslog(LOG_ERR, "%s", message);

#ifdef DEBUG
		fprintf(stderr, "%s", message);
		fprintf(stderr, "%s\n", config_error_text(&cfg));
#endif

		config_destroy(&cfg);  
		return;
	}

	readconfig_value(&cfg, "url_passwd", &url_passwd);
	readconfig_value(&cfg, "url_group", &url_group);
	readconfig_value(&cfg, "cafile", &cafile);
	readconfig_value(&cfg, "sslcheck", &sslcheck);
	readconfig_value(&cfg, "username", &username);
	readconfig_value(&cfg, "password", &password);

	if (get_cookies() == NULL) {
		int		i = 0;
		regex_t	envVar;
		readconfig_value(&cfg, "cookie_num", &val);
		if (val == NULL) goto end_config;
		int num_cookies = atoi(val);

		if(regcomp(&envVar, "\\$\\{[a-zA-Z0-9_-]+\\}", REG_EXTENDED))
			goto end_config;

		for(i=1; i <= num_cookies; ++i) {
			char 	strkey[512],
				*cookie_name = NULL,
				*cookie_value = NULL;

			sprintf(strkey, "cookie_%d_name", i);
			readconfig_value(&cfg, strkey, (const char **)&cookie_name);
			sprintf(strkey, "cookie_%d_value", i);
			readconfig_value(&cfg, strkey, (const char **)&cookie_value);
			if(!cookie_value || !cookie_name)
				goto on_gen_err;
			/* expand cookie_name */
			char		*tmpVal = cookie_name,
					tmpBuf[2048];
			tmpBuf[0] = '\0';
			regmatch_t	match; 
			while(!regexec(&envVar, tmpVal, 1, &match, 0)) {
				if(-1 == match.rm_so)
					break;
				strncat(tmpBuf, tmpVal, match.rm_so);
				char            *pEnd = &tmpVal[match.rm_eo-1],
						tVal = *pEnd;
				*pEnd = '\0';
				const char	*envVal = getenv(tmpVal + match.rm_so + 2);
				*pEnd = tVal;
				tmpVal += match.rm_eo;
				if(envVal) strcat(tmpBuf, envVal);
			}
			free(cookie_name);
			cookie_name = strdup(tmpBuf);
			if(!cookie_name) goto on_gen_err;
			/* do the same for cookie_value */
			tmpVal = cookie_value;
			tmpBuf[0] = '\0';
			while(!regexec(&envVar, tmpVal, 1, &match, 0)) {
				if(-1 == match.rm_so)
					break;
				strncat(tmpBuf, tmpVal, match.rm_so);
				char            *pEnd = &tmpVal[match.rm_eo-1],
						tVal = *pEnd;
				*pEnd = '\0';
				const char	*envVal = getenv(tmpVal + match.rm_so + 2);
				*pEnd = tVal;
				tmpVal += match.rm_eo;
				if(envVal) strcat(tmpBuf, envVal);
			}
			free(cookie_value);
			cookie_value = strdup(tmpBuf);
			if(!cookie_value) goto on_gen_err;
			/* add to cookies */
			BODY *curcookie = (BODY *) malloc(sizeof(BODY));
			if(!curcookie) goto on_gen_err;
			curcookie->row = (char *) malloc(strlen(cookie_name)+strlen(cookie_value)+2);
			if(!curcookie->row) {
				free(curcookie);
				goto on_gen_err;
			}
			sprintf(curcookie->row, "%s=%s", cookie_name, cookie_value);
			curcookie->next = get_cookies();
			set_cookies(curcookie);

#ifdef DEBUG
			fprintf(stderr, "Read new cookie: [%s] => %s\n", cookie_name, cookie_value);
#endif

on_gen_err:
			if(cookie_value) free(cookie_value);
			if(cookie_name) free(cookie_name);
		}
		regfree(&envVar);
	}

end_config:
	config_destroy(&cfg);
	if(val) free((void*)val); 
}
Example #3
0
void process_request() {
  set_cookies();
  route_request();
  free_params();
}