std::shared_ptr<Response> request(const std::string& request_type, const std::string& path, std::iostream& content, const std::map<std::string, std::string>& header = std::map<std::string, std::string>()) { std::string corrected_path = path; if (corrected_path == "") corrected_path = "/"; content.seekp(0, std::ios::end); auto content_length = content.tellp(); content.seekp(0, std::ios::beg); boost::asio::streambuf write_buffer; std::ostream write_stream(&write_buffer); write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << "Host: " << host << "\r\n"; for (auto& h : header) { write_stream << h.first << ": " << h.second << "\r\n"; } if (content_length>0) write_stream << "Content-Length: " << content_length << "\r\n"; write_stream << "\r\n"; if (content_length>0) write_stream << content.rdbuf(); try { connect(); boost::asio::write(*socket, write_buffer); } catch (const std::exception& e) { socket_error = true; throw std::invalid_argument(e.what()); } return request_read(); }
std::shared_ptr<Response> request(const std::string& request_type, const std::string& path = "/", boost::string_ref content = "", const std::map<std::string, std::string>& header = std::map<std::string, std::string>()) { std::string corrected_path = path; if (corrected_path == "") corrected_path = "/"; boost::asio::streambuf write_buffer; std::ostream write_stream(&write_buffer); write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << "Host: " << host << "\r\n"; for (auto& h : header) { write_stream << h.first << ": " << h.second << "\r\n"; } if (content.size()>0) write_stream << "Content-Length: " << content.size() << "\r\n"; write_stream << "\r\n"; try { connect(); boost::asio::write(*socket, write_buffer); if (content.size()>0) boost::asio::write(*socket, boost::asio::buffer(content.data(), content.size())); } catch (const std::exception& e) { socket_error = true; throw std::invalid_argument(e.what()); } return request_read(); }
int ac_rtld_config::process_map_file(unsigned int fd, hash_node **hashtable) { unsigned int i, num1, num2; char buffer[20]; unsigned int line = 0; buffer[0] = 'a'; while (buffer[0] != '\0') { char* endptr; line++; request_read(fd, buffer, 1); /* Ignore line */ if (buffer[0] == '#') { while (buffer[0] != '\n' && buffer[0] != '\0') request_read(fd, buffer, 1); continue; } /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1); /* Nothing to parse here, go to next line */ if (buffer[0] == '\n' || buffer[0] == '\0') continue; /** Parse num = num statement **/ /* Not a number */ if (buffer[0] < '0' || buffer[0] > '9') { return ERROR; } i = 0; while (buffer[i] >= '0' && buffer[i] <= '9') { request_read(fd, &buffer[++i], 1); if (i >= 20) { return ERROR; } } /* Unexpected end of line */ if (buffer[i] == '\n' || buffer[i] == '\0') { return ERROR; } endptr = NULL; num1 = strtol(buffer, &endptr, 10); if (endptr != &buffer[i]) { return ERROR; } buffer[0] = buffer[i]; /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1); /* Unexpected token */ if (buffer[0] != '=') { return ERROR; } request_read(fd, buffer, 1); /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1); /* Unexpected end of line */ if (buffer[0] == '\n' || buffer[0] == '\0') { return ERROR; } /* Parse the second number at num = num statement */ /* Not a number */ if (buffer[0] < '0' || buffer[0] > '9') { return ERROR; } i = 0; while (buffer[i] >= '0' && buffer[i] <= '9') { request_read(fd, &buffer[++i], 1); if (i >= 20) { return ERROR; } } endptr = NULL; num2 = strtol(buffer, &endptr, 10); if (endptr != &buffer[i]) { return ERROR; } buffer[0] = buffer[i]; if (buffer[0] != '\n' && buffer[0] != '\0') { return ERROR; } if (hash_add_value(hashtable, num1, num2) == ERROR) { return ERROR; } } return FINE; }
int process_map_file(unsigned int fd, hash_node **hashtable, char reverse_mode) { unsigned int i, num1, num2; char buffer[20]; unsigned int line = 0; buffer[0] = 'a'; while (buffer[0] != '\0') { char* endptr; line++; request_read(fd, buffer, 1, FALSE); /* Ignore line */ if (buffer[0] == '#') { while (buffer[0] != '\n' && buffer[0] != '\0') request_read(fd, buffer, 1, FALSE); continue; } /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1, FALSE); /* Nothing to parse here, go to next line */ if (buffer[0] == '\n' || buffer[0] == '\0') continue; /** Parse num = num statement **/ /* Not a number */ if (buffer[0] < '0' || buffer[0] > '9') { fprintf(stderr, "Error while parsing map file: expecting number = number statement at line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } i = 0; while (buffer[i] >= '0' && buffer[i] <= '9') { request_read(fd, &buffer[++i], 1, FALSE); if (i >= 20) { fprintf(stderr, "Error while parsing map file: number too large at line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } } /* Unexpected end of line */ if (buffer[i] == '\n' || buffer[i] == '\0') { fprintf(stderr, "Error while parsing map file: unexpected end of line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } endptr = NULL; num1 = strtol(buffer, &endptr, 10); if (endptr != &buffer[i]) { fprintf(stderr, "Error while parsing map file: first number of line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } buffer[0] = buffer[i]; /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1, FALSE); /* Unexpected token */ if (buffer[0] != '=') { fprintf(stderr, "Error while parsing map file: expected token \"=\" at line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } request_read(fd, buffer, 1, FALSE); /* Skips whitespace */ while (buffer[0] == ' ' || buffer[0] == '\t') request_read(fd, buffer, 1, FALSE); /* Unexpected end of line */ if (buffer[0] == '\n' || buffer[0] == '\0') { fprintf(stderr, "Error while parsing map file: unexpected end of line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } /* Parse the second number at num = num statement */ /* Not a number */ if (buffer[0] < '0' || buffer[0] > '9') { fprintf(stderr, "Error while parsing map file: expecting number = number statement at line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } i = 0; while (buffer[i] >= '0' && buffer[i] <= '9') { request_read(fd, &buffer[++i], 1, FALSE); if (i >= 20) { fprintf(stderr, "Error while parsing map file: number too large at line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } } endptr = NULL; num2 = strtol(buffer, &endptr, 10); if (endptr != &buffer[i]) { fprintf(stderr, "Error while parsing map file: second number of line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } buffer[0] = buffer[i]; if (buffer[0] != '\n' && buffer[0] != '\0') { fprintf(stderr, "Error while parsing map file: junk at end of line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } if (!reverse_mode) { if (hash_add_value(hashtable, num1, num2) == ACRELCONVERT_FUNC_ERROR) { fprintf(stderr, "Error while parsing map file: failed to add hash value corresponding to line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } } else /* Reverse map */ { if (hash_add_value(hashtable, num2, num1) == ACRELCONVERT_FUNC_ERROR) { fprintf(stderr, "Error while parsing map file: failed to add hash value corresponding to line %d\n", line); return ACRELCONVERT_FUNC_ERROR; } } } return ACRELCONVERT_FUNC_OK; }