return ret;
}
-int fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int min_limit, int max_limit)
+string fetch_config_string(const vector<ConfigLine> &config, const string &keyword,
+ ParameterType parameter_type, const string &default_value)
{
+ assert(parameter_type == PARAMATER_MANDATORY || parameter_type == PARAMETER_OPTIONAL);
+ for (unsigned i = 0; i < config.size(); ++i) {
+ if (config[i].keyword != keyword) {
+ continue;
+ }
+ if (config[i].parameters.size() > 0 ||
+ config[i].arguments.size() != 1) {
+ fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
+ exit(1);
+ }
+ return config[i].arguments[0];
+ }
+ if (parameter_type == PARAMATER_MANDATORY) {
+ fprintf(stderr, "ERROR: Missing '%s' statement in config file.\n",
+ keyword.c_str());
+ exit(1);
+ } else {
+ return default_value;
+ }
+}
+
+int fetch_config_int(const std::vector<ConfigLine> &config, const std::string &keyword,
+ int min_limit, int max_limit,
+ ParameterType parameter_type, int default_value)
+{
+ assert(parameter_type == PARAMATER_MANDATORY || parameter_type == PARAMETER_OPTIONAL);
bool value_found = false;
int value = -1;
for (unsigned i = 0; i < config.size(); ++i) {
value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
}
if (!value_found) {
+ if (parameter_type == PARAMETER_OPTIONAL) {
+ return default_value;
+ }
fprintf(stderr, "ERROR: Missing '%s' statement in config file.\n",
keyword.c_str());
exit(1);
}
return value;
}
+
+#define MAX_REQUEST_SIZE 16384 /* 16 kB. */
+
+RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
+{
+ // Guard against overlong requests gobbling up all of our space.
+ if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
+ return RP_OUT_OF_SPACE;
+ }
+
+ // See if we have \r\n\r\n anywhere in the request. We start three bytes
+ // before what we just appended, in case we just got the final character.
+ size_t existing_data_bytes = existing_data->size();
+ existing_data->append(string(new_data, new_data + new_data_size));
+
+ const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
+ const char *ptr = reinterpret_cast<char *>(
+ memmem(existing_data->data() + start_at, existing_data->size() - start_at,
+ "\r\n\r\n", 4));
+ if (ptr == NULL) {
+ return RP_NOT_FINISHED_YET;
+ }
+ if (ptr != existing_data->data() + existing_data->size() - 4) {
+ return RP_EXTRA_DATA;
+ }
+ return RP_FINISHED;
+}