Major string_view refactor. Added namespace sv
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
#include "http.hpp"
|
||||
#include "wrap-sstream.hpp"
|
||||
#include "wrap-string.hpp"
|
||||
#include "util.hpp"
|
||||
#include "luastack.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
static void url_encode(const eng::string &value, StreamBuffer *sb) {
|
||||
@@ -10,7 +12,7 @@ static void url_encode(const eng::string &value, StreamBuffer *sb) {
|
||||
for (int i = 0; i < int(value.size()); i++) {
|
||||
char c = value[i];
|
||||
|
||||
if (util::ascii_isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' || (c == '/')) {
|
||||
if (sv::ascii_isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' || (c == '/')) {
|
||||
sb->write_char(c);
|
||||
} else if (c == ' ') {
|
||||
sb->write_char('+');
|
||||
@@ -77,7 +79,7 @@ void HttpRequest::set_host(const eng::string &s) {
|
||||
// This is not quite strict, but it's close. I believe
|
||||
// the DNS lookup will fail for invalid hostnames anyway.
|
||||
for (char c : host) {
|
||||
if ((c != '-') && (c != '.') && (!util::ascii_isalnum(c))) {
|
||||
if ((c != '-') && (c != '.') && (!sv::ascii_isalnum(c))) {
|
||||
ErrorStringStream error(&error_);
|
||||
error << "HTTPS hostnames can only contain letters, digits, and hyphen: " << host;
|
||||
return;
|
||||
@@ -106,11 +108,11 @@ void HttpRequest::set_port(int port) {
|
||||
}
|
||||
|
||||
void HttpRequest::set_url(const eng::string &url) {
|
||||
if (util::has_prefix(url, "https://")) {
|
||||
if (sv::has_prefix(url, "https://")) {
|
||||
ErrorStringStream error(&error_);
|
||||
error << "set_url(full_url) not implemented yet.";
|
||||
return;
|
||||
} else if (util::has_prefix(url, "/")) {
|
||||
} else if (sv::has_prefix(url, "/")) {
|
||||
if (!path_.empty()) {
|
||||
ErrorStringStream error(&error_);
|
||||
error << "HTTP path specified twice: " << path_ << " and " << url;
|
||||
@@ -338,11 +340,6 @@ void HttpResponse::fail(int response_code, const eng::string &error) {
|
||||
content_ = "";
|
||||
}
|
||||
|
||||
static std::string_view readline(std::string_view &v) {
|
||||
std::string_view result = util::sv_split_one(v, '\n');
|
||||
return util::sv_rtrim(result, '\r');
|
||||
}
|
||||
|
||||
void HttpResponse::parse(const StreamBuffer *sb) {
|
||||
// We're not going to modify the StreamBuffer at all.
|
||||
// Instead, we work entirely on a view.
|
||||
@@ -355,7 +352,7 @@ void HttpResponse::parse(const StreamBuffer *sb) {
|
||||
}
|
||||
|
||||
// Parse the status line.
|
||||
std::string_view status = readline(view);
|
||||
std::string_view status = sv::read_to_line(view);
|
||||
if (status.empty()) {
|
||||
fail(500, "HTTP status-line not present in response");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user