mirror of
https://github.com/hrydgard/ppsspp.git
synced 2024-11-26 23:10:38 +00:00
Common: Cleanup some mismatched indentation.
Also move initialization from constructors in a few places.
This commit is contained in:
parent
9894c74bef
commit
7e21e9a721
@ -8,53 +8,53 @@
|
||||
// Acts as a queue. Intended to be as fast as possible for most uses.
|
||||
// Does not do synchronization, must use external mutexes.
|
||||
class Buffer {
|
||||
public:
|
||||
Buffer();
|
||||
~Buffer();
|
||||
public:
|
||||
Buffer();
|
||||
~Buffer();
|
||||
|
||||
// Write max [length] bytes to the returned pointer.
|
||||
// Any other operation on this Buffer invalidates the pointer.
|
||||
char *Append(size_t length);
|
||||
// Write max [length] bytes to the returned pointer.
|
||||
// Any other operation on this Buffer invalidates the pointer.
|
||||
char *Append(size_t length);
|
||||
|
||||
// These work pretty much like you'd expect.
|
||||
void Append(const char *str); // str null-terminated. The null is not copied.
|
||||
void Append(const std::string &str);
|
||||
void Append(const Buffer &other);
|
||||
|
||||
// Various types. Useful for varz etc. Appends a string representation of the
|
||||
// value, rather than a binary representation.
|
||||
void AppendValue(int value);
|
||||
// Various types. Useful for varz etc. Appends a string representation of the
|
||||
// value, rather than a binary representation.
|
||||
void AppendValue(int value);
|
||||
|
||||
// Parsing Helpers
|
||||
|
||||
// Use for easy line skipping. If no CRLF within the buffer, returns -1.
|
||||
// If parsing HTML headers, this indicates that you should probably buffer up
|
||||
// more data.
|
||||
int OffsetToAfterNextCRLF();
|
||||
// Parsing Helpers
|
||||
|
||||
// Takers
|
||||
// Use for easy line skipping. If no CRLF within the buffer, returns -1.
|
||||
// If parsing HTML headers, this indicates that you should probably buffer up
|
||||
// more data.
|
||||
int OffsetToAfterNextCRLF();
|
||||
|
||||
void Take(size_t length, std::string *dest);
|
||||
void Take(size_t length, char *dest);
|
||||
void TakeAll(std::string *dest) { Take(size(), dest); }
|
||||
// On failure, return value < 0 and *dest is unchanged.
|
||||
// Strips off the actual CRLF from the result.
|
||||
int TakeLineCRLF(std::string *dest);
|
||||
// Takers
|
||||
|
||||
// Skippers
|
||||
void Skip(size_t length);
|
||||
// Returns -1 on failure (no CRLF within sight).
|
||||
// Otherwise returns the length of the line skipped, not including CRLF. Can be 0.
|
||||
int SkipLineCRLF();
|
||||
void Take(size_t length, std::string *dest);
|
||||
void Take(size_t length, char *dest);
|
||||
void TakeAll(std::string *dest) { Take(size(), dest); }
|
||||
// On failure, return value < 0 and *dest is unchanged.
|
||||
// Strips off the actual CRLF from the result.
|
||||
int TakeLineCRLF(std::string *dest);
|
||||
|
||||
// Utility functions.
|
||||
void Printf(const char *fmt, ...);
|
||||
// Skippers
|
||||
void Skip(size_t length);
|
||||
// Returns -1 on failure (no CRLF within sight).
|
||||
// Otherwise returns the length of the line skipped, not including CRLF. Can be 0.
|
||||
int SkipLineCRLF();
|
||||
|
||||
// Dumps the entire buffer to the string, but keeps it around.
|
||||
// Only to be used for debugging, since it might not be fast at all.
|
||||
void PeekAll(std::string *dest);
|
||||
// Utility functions.
|
||||
void Printf(const char *fmt, ...);
|
||||
|
||||
// Simple I/O.
|
||||
// Dumps the entire buffer to the string, but keeps it around.
|
||||
// Only to be used for debugging, since it might not be fast at all.
|
||||
void PeekAll(std::string *dest);
|
||||
|
||||
// Simple I/O.
|
||||
|
||||
// Writes the entire buffer to the file descriptor. Also resets the
|
||||
// size to zero. On failure, data remains in buffer and nothing is
|
||||
@ -63,21 +63,21 @@ class Buffer {
|
||||
bool FlushToFile(const char *filename);
|
||||
bool FlushSocket(uintptr_t sock, double timeout = -1.0, bool *cancelled = nullptr); // Windows portability
|
||||
|
||||
bool ReadAll(int fd, int hintSize = 0);
|
||||
bool ReadAllWithProgress(int fd, int knownSize, float *progress, bool *cancelled);
|
||||
bool ReadAll(int fd, int hintSize = 0);
|
||||
bool ReadAllWithProgress(int fd, int knownSize, float *progress, bool *cancelled);
|
||||
|
||||
// < 0: error
|
||||
// >= 0: number of bytes read
|
||||
int Read(int fd, size_t sz);
|
||||
int Read(int fd, size_t sz);
|
||||
|
||||
// Utilities. Try to avoid checking for size.
|
||||
size_t size() const { return data_.size(); }
|
||||
bool empty() const { return size() == 0; }
|
||||
void clear() { data_.resize(0); }
|
||||
// Utilities. Try to avoid checking for size.
|
||||
size_t size() const { return data_.size(); }
|
||||
bool empty() const { return size() == 0; }
|
||||
void clear() { data_.resize(0); }
|
||||
|
||||
private:
|
||||
// TODO: Find a better internal representation, like a cord.
|
||||
std::vector<char> data_;
|
||||
private:
|
||||
// TODO: Find a better internal representation, like a cord.
|
||||
std::vector<char> data_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Buffer);
|
||||
DISALLOW_COPY_AND_ASSIGN(Buffer);
|
||||
};
|
||||
|
@ -4,21 +4,21 @@ template <typename T, typename U>
|
||||
class InitConstMap
|
||||
{
|
||||
private:
|
||||
std::map<T, U> m_map;
|
||||
std::map<T, U> m_map;
|
||||
public:
|
||||
InitConstMap(const T& key, const U& val)
|
||||
{
|
||||
m_map[key] = val;
|
||||
}
|
||||
InitConstMap(const T& key, const U& val)
|
||||
{
|
||||
m_map[key] = val;
|
||||
}
|
||||
|
||||
InitConstMap<T, U>& operator()(const T& key, const U& val)
|
||||
{
|
||||
m_map[key] = val;
|
||||
return *this;
|
||||
}
|
||||
InitConstMap<T, U>& operator()(const T& key, const U& val)
|
||||
{
|
||||
m_map[key] = val;
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator std::map<T, U>()
|
||||
{
|
||||
return m_map;
|
||||
}
|
||||
operator std::map<T, U>()
|
||||
{
|
||||
return m_map;
|
||||
}
|
||||
};
|
||||
|
@ -7,31 +7,31 @@ namespace hash {
|
||||
#define MOD_ADLER 65521
|
||||
// data: Pointer to the data to be summed; len is in bytes
|
||||
uint32_t Adler32(const uint8_t *data, size_t len) {
|
||||
uint32_t a = 1, b = 0;
|
||||
while (len) {
|
||||
size_t tlen = len > 5550 ? 5550 : len;
|
||||
len -= tlen;
|
||||
do {
|
||||
a += *data++;
|
||||
b += a;
|
||||
} while (--tlen);
|
||||
uint32_t a = 1, b = 0;
|
||||
while (len) {
|
||||
size_t tlen = len > 5550 ? 5550 : len;
|
||||
len -= tlen;
|
||||
do {
|
||||
a += *data++;
|
||||
b += a;
|
||||
} while (--tlen);
|
||||
|
||||
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
|
||||
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
|
||||
}
|
||||
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
|
||||
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
|
||||
}
|
||||
|
||||
// It can be shown that a <= 0x1013a here, so a single subtract will do.
|
||||
if (a >= MOD_ADLER) {
|
||||
a -= MOD_ADLER;
|
||||
}
|
||||
// It can be shown that a <= 0x1013a here, so a single subtract will do.
|
||||
if (a >= MOD_ADLER) {
|
||||
a -= MOD_ADLER;
|
||||
}
|
||||
|
||||
// It can be shown that b can reach 0xfff87 here.
|
||||
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
|
||||
// It can be shown that b can reach 0xfff87 here.
|
||||
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
|
||||
|
||||
if (b >= MOD_ADLER) {
|
||||
b -= MOD_ADLER;
|
||||
}
|
||||
return (b << 16) | a;
|
||||
if (b >= MOD_ADLER) {
|
||||
b -= MOD_ADLER;
|
||||
}
|
||||
return (b << 16) | a;
|
||||
}
|
||||
|
||||
} // namespace hash
|
||||
|
@ -1,8 +1,8 @@
|
||||
#include "ppsspp_config.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#ifndef _WIN32
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
@ -18,8 +18,8 @@
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "Common/Common.h"
|
||||
#include "Common/Log.h"
|
||||
#include "Common/File/FileDescriptor.h"
|
||||
#include "Common/Log.h"
|
||||
|
||||
namespace fd_util {
|
||||
|
||||
@ -27,100 +27,99 @@ namespace fd_util {
|
||||
// Reads from a socket, up to an '\n'. This means that if the line ends
|
||||
// with '\r', the '\r' will be returned.
|
||||
size_t ReadLine(int fd, char *vptr, size_t buf_size) {
|
||||
char *buffer = vptr;
|
||||
size_t n;
|
||||
for (n = 1; n < buf_size; n++) {
|
||||
char c;
|
||||
size_t rc;
|
||||
if ((rc = read(fd, &c, 1)) == 1) {
|
||||
*buffer++ = c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
else if (rc == 0) {
|
||||
if (n == 1)
|
||||
return 0;
|
||||
else
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
_assert_msg_(false, "Error in Readline()");
|
||||
}
|
||||
}
|
||||
char *buffer = vptr;
|
||||
size_t n;
|
||||
for (n = 1; n < buf_size; n++) {
|
||||
char c;
|
||||
size_t rc;
|
||||
if ((rc = read(fd, &c, 1)) == 1) {
|
||||
*buffer++ = c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
} else if (rc == 0) {
|
||||
if (n == 1)
|
||||
return 0;
|
||||
else
|
||||
break;
|
||||
} else {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
_assert_msg_(false, "Error in Readline()");
|
||||
}
|
||||
}
|
||||
|
||||
*buffer = 0;
|
||||
return n;
|
||||
*buffer = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
// Misnamed, it just writes raw data in a retry loop.
|
||||
size_t WriteLine(int fd, const char *vptr, size_t n) {
|
||||
const char *buffer = vptr;
|
||||
size_t nleft = n;
|
||||
const char *buffer = vptr;
|
||||
size_t nleft = n;
|
||||
|
||||
while (nleft > 0) {
|
||||
int nwritten;
|
||||
if ((nwritten = (int)write(fd, buffer, (unsigned int)nleft)) <= 0) {
|
||||
if (errno == EINTR)
|
||||
nwritten = 0;
|
||||
else
|
||||
_assert_msg_(false, "Error in Writeline()");
|
||||
}
|
||||
nleft -= nwritten;
|
||||
buffer += nwritten;
|
||||
}
|
||||
while (nleft > 0) {
|
||||
int nwritten;
|
||||
if ((nwritten = (int)write(fd, buffer, (unsigned int)nleft)) <= 0) {
|
||||
if (errno == EINTR)
|
||||
nwritten = 0;
|
||||
else
|
||||
_assert_msg_(false, "Error in Writeline()");
|
||||
}
|
||||
nleft -= nwritten;
|
||||
buffer += nwritten;
|
||||
}
|
||||
|
||||
return n;
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t WriteLine(int fd, const char *buffer) {
|
||||
return WriteLine(fd, buffer, strlen(buffer));
|
||||
return WriteLine(fd, buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
size_t Write(int fd, const std::string &str) {
|
||||
return WriteLine(fd, str.c_str(), str.size());
|
||||
return WriteLine(fd, str.c_str(), str.size());
|
||||
}
|
||||
|
||||
bool WaitUntilReady(int fd, double timeout, bool for_write) {
|
||||
struct timeval tv;
|
||||
tv.tv_sec = floor(timeout);
|
||||
tv.tv_usec = (timeout - floor(timeout)) * 1000000.0;
|
||||
struct timeval tv;
|
||||
tv.tv_sec = floor(timeout);
|
||||
tv.tv_usec = (timeout - floor(timeout)) * 1000000.0;
|
||||
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
// First argument to select is the highest socket in the set + 1.
|
||||
int rval;
|
||||
if (for_write) {
|
||||
rval = select(fd + 1, NULL, &fds, NULL, &tv);
|
||||
} else {
|
||||
rval = select(fd + 1, &fds, NULL, NULL, &tv);
|
||||
}
|
||||
if (rval < 0) {
|
||||
// Error calling select.
|
||||
return false;
|
||||
} else if (rval == 0) {
|
||||
// Timeout.
|
||||
return false;
|
||||
} else {
|
||||
// Socket is ready.
|
||||
return true;
|
||||
}
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
// First argument to select is the highest socket in the set + 1.
|
||||
int rval;
|
||||
if (for_write) {
|
||||
rval = select(fd + 1, nullptr, &fds, nullptr, &tv);
|
||||
} else {
|
||||
rval = select(fd + 1, &fds, nullptr, nullptr, &tv);
|
||||
}
|
||||
|
||||
if (rval < 0) {
|
||||
// Error calling select.
|
||||
return false;
|
||||
} else if (rval == 0) {
|
||||
// Timeout.
|
||||
return false;
|
||||
} else {
|
||||
// Socket is ready.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void SetNonBlocking(int sock, bool non_blocking) {
|
||||
#ifndef _WIN32
|
||||
int opts = fcntl(sock, F_GETFL);
|
||||
if (opts < 0) {
|
||||
if (opts < 0) {
|
||||
perror("fcntl(F_GETFL)");
|
||||
ERROR_LOG(IO, "Error getting socket status while changing nonblocking status");
|
||||
}
|
||||
if (non_blocking) {
|
||||
opts = (opts | O_NONBLOCK);
|
||||
} else {
|
||||
opts = (opts & ~O_NONBLOCK);
|
||||
}
|
||||
opts = (opts | O_NONBLOCK);
|
||||
} else {
|
||||
opts = (opts & ~O_NONBLOCK);
|
||||
}
|
||||
|
||||
if (fcntl(sock, F_SETFL, opts) < 0) {
|
||||
perror("fcntl(F_SETFL)");
|
||||
|
@ -36,13 +36,12 @@
|
||||
|
||||
namespace net {
|
||||
|
||||
Connection::Connection()
|
||||
: port_(-1), resolved_(NULL), sock_(-1) {
|
||||
Connection::Connection() {
|
||||
}
|
||||
|
||||
Connection::~Connection() {
|
||||
Disconnect();
|
||||
if (resolved_ != NULL)
|
||||
if (resolved_ != nullptr)
|
||||
DNSResolveFree(resolved_);
|
||||
}
|
||||
|
||||
|
@ -29,12 +29,12 @@ protected:
|
||||
// Store the remote host here, so we can send it along through HTTP/1.1 requests.
|
||||
// TODO: Move to http::client?
|
||||
std::string host_;
|
||||
int port_;
|
||||
int port_ = -1;
|
||||
|
||||
addrinfo *resolved_;
|
||||
addrinfo *resolved_ = nullptr;
|
||||
|
||||
private:
|
||||
uintptr_t sock_;
|
||||
uintptr_t sock_ = -1;
|
||||
|
||||
};
|
||||
|
||||
|
@ -12,34 +12,32 @@
|
||||
|
||||
namespace http {
|
||||
|
||||
RequestHeader::RequestHeader()
|
||||
: status(200), referer(0), user_agent(0),
|
||||
resource(0), params(0), content_length(-1), first_header_(true) {
|
||||
RequestHeader::RequestHeader() {
|
||||
}
|
||||
|
||||
RequestHeader::~RequestHeader() {
|
||||
delete [] referer;
|
||||
delete [] user_agent;
|
||||
delete [] resource;
|
||||
delete [] params;
|
||||
delete [] referer;
|
||||
delete [] user_agent;
|
||||
delete [] resource;
|
||||
delete [] params;
|
||||
}
|
||||
|
||||
bool RequestHeader::GetParamValue(const char *param_name, std::string *value) const {
|
||||
if (!params)
|
||||
return false;
|
||||
std::string p(params);
|
||||
std::vector<std::string> v;
|
||||
if (!params)
|
||||
return false;
|
||||
std::string p(params);
|
||||
std::vector<std::string> v;
|
||||
SplitString(p, '&', v);
|
||||
for (size_t i = 0; i < v.size(); i++) {
|
||||
std::vector<std::string> parts;
|
||||
for (size_t i = 0; i < v.size(); i++) {
|
||||
std::vector<std::string> parts;
|
||||
SplitString(v[i], '=', parts);
|
||||
DEBUG_LOG(IO, "Param: %s Value: %s", parts[0].c_str(), parts[1].c_str());
|
||||
if (parts[0] == param_name) {
|
||||
*value = parts[1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
DEBUG_LOG(IO, "Param: %s Value: %s", parts[0].c_str(), parts[1].c_str());
|
||||
if (parts[0] == param_name) {
|
||||
*value = parts[1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RequestHeader::GetOther(const char *name, std::string *value) const {
|
||||
@ -54,87 +52,87 @@ bool RequestHeader::GetOther(const char *name, std::string *value) const {
|
||||
// Intended to be a mad fast parser. It's not THAT fast currently, there's still
|
||||
// things to optimize, but meh.
|
||||
int RequestHeader::ParseHttpHeader(const char *buffer) {
|
||||
if (first_header_) {
|
||||
// Step 1: Method
|
||||
first_header_ = false;
|
||||
if (!memcmp(buffer, "GET ", 4)) {
|
||||
method = GET;
|
||||
buffer += 4;
|
||||
} else if (!memcmp(buffer, "HEAD ", 5)) {
|
||||
method = HEAD;
|
||||
buffer += 5;
|
||||
} else if (!memcmp(buffer, "POST ", 5)) {
|
||||
method = POST;
|
||||
buffer += 5;
|
||||
} else {
|
||||
method = UNSUPPORTED;
|
||||
status = 405;
|
||||
return -1;
|
||||
}
|
||||
SkipSpace(&buffer);
|
||||
if (first_header_) {
|
||||
// Step 1: Method
|
||||
first_header_ = false;
|
||||
if (!memcmp(buffer, "GET ", 4)) {
|
||||
method = GET;
|
||||
buffer += 4;
|
||||
} else if (!memcmp(buffer, "HEAD ", 5)) {
|
||||
method = HEAD;
|
||||
buffer += 5;
|
||||
} else if (!memcmp(buffer, "POST ", 5)) {
|
||||
method = POST;
|
||||
buffer += 5;
|
||||
} else {
|
||||
method = UNSUPPORTED;
|
||||
status = 405;
|
||||
return -1;
|
||||
}
|
||||
SkipSpace(&buffer);
|
||||
|
||||
// Step 2: Resource, params (what's after the ?, if any)
|
||||
const char *endptr = strchr(buffer, ' ');
|
||||
const char *q_ptr = strchr(buffer, '?');
|
||||
// Step 2: Resource, params (what's after the ?, if any)
|
||||
const char *endptr = strchr(buffer, ' ');
|
||||
const char *q_ptr = strchr(buffer, '?');
|
||||
|
||||
int resource_name_len;
|
||||
if (q_ptr)
|
||||
resource_name_len = q_ptr - buffer;
|
||||
else
|
||||
resource_name_len = endptr - buffer;
|
||||
if (!resource_name_len) {
|
||||
status = 400;
|
||||
return -1;
|
||||
}
|
||||
resource = new char[resource_name_len + 1];
|
||||
memcpy(resource, buffer, resource_name_len);
|
||||
resource[resource_name_len] = '\0';
|
||||
if (q_ptr) {
|
||||
int param_length = endptr - q_ptr - 1;
|
||||
params = new char[param_length + 1];
|
||||
memcpy(params, q_ptr + 1, param_length);
|
||||
params[param_length] = '\0';
|
||||
}
|
||||
if (strstr(buffer, "HTTP/"))
|
||||
type = FULL;
|
||||
else
|
||||
type = SIMPLE;
|
||||
return 0;
|
||||
}
|
||||
int resource_name_len;
|
||||
if (q_ptr)
|
||||
resource_name_len = q_ptr - buffer;
|
||||
else
|
||||
resource_name_len = endptr - buffer;
|
||||
if (!resource_name_len) {
|
||||
status = 400;
|
||||
return -1;
|
||||
}
|
||||
resource = new char[resource_name_len + 1];
|
||||
memcpy(resource, buffer, resource_name_len);
|
||||
resource[resource_name_len] = '\0';
|
||||
if (q_ptr) {
|
||||
int param_length = endptr - q_ptr - 1;
|
||||
params = new char[param_length + 1];
|
||||
memcpy(params, q_ptr + 1, param_length);
|
||||
params[param_length] = '\0';
|
||||
}
|
||||
if (strstr(buffer, "HTTP/"))
|
||||
type = FULL;
|
||||
else
|
||||
type = SIMPLE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// We have a real header to parse.
|
||||
const char *colon = strchr(buffer, ':');
|
||||
if (!colon) {
|
||||
status = 400;
|
||||
return -1;
|
||||
}
|
||||
// We have a real header to parse.
|
||||
const char *colon = strchr(buffer, ':');
|
||||
if (!colon) {
|
||||
status = 400;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The header is formatted as key: value.
|
||||
int key_len = colon - buffer;
|
||||
const char *key = buffer;
|
||||
// The header is formatted as key: value.
|
||||
int key_len = colon - buffer;
|
||||
const char *key = buffer;
|
||||
|
||||
// Go to after the colon to get the value.
|
||||
buffer = colon + 1;
|
||||
SkipSpace(&buffer);
|
||||
int value_len = (int)strlen(buffer);
|
||||
|
||||
if (!strncasecmp(key, "User-Agent", key_len)) {
|
||||
user_agent = new char[value_len + 1];
|
||||
memcpy(user_agent, buffer, value_len + 1);
|
||||
VERBOSE_LOG(IO, "user-agent: %s", user_agent);
|
||||
} else if (!strncasecmp(key, "Referer", key_len)) {
|
||||
referer = new char[value_len + 1];
|
||||
memcpy(referer, buffer, value_len + 1);
|
||||
} else if (!strncasecmp(key, "Content-Length", key_len)) {
|
||||
content_length = atoi(buffer);
|
||||
VERBOSE_LOG(IO, "Content-Length: %i", (int)content_length);
|
||||
} else {
|
||||
std::string key_str(key, key_len);
|
||||
std::transform(key_str.begin(), key_str.end(), key_str.begin(), tolower);
|
||||
other[key_str] = buffer;
|
||||
}
|
||||
// Go to after the colon to get the value.
|
||||
buffer = colon + 1;
|
||||
SkipSpace(&buffer);
|
||||
int value_len = (int)strlen(buffer);
|
||||
|
||||
return 0;
|
||||
if (!strncasecmp(key, "User-Agent", key_len)) {
|
||||
user_agent = new char[value_len + 1];
|
||||
memcpy(user_agent, buffer, value_len + 1);
|
||||
VERBOSE_LOG(IO, "user-agent: %s", user_agent);
|
||||
} else if (!strncasecmp(key, "Referer", key_len)) {
|
||||
referer = new char[value_len + 1];
|
||||
memcpy(referer, buffer, value_len + 1);
|
||||
} else if (!strncasecmp(key, "Content-Length", key_len)) {
|
||||
content_length = atoi(buffer);
|
||||
VERBOSE_LOG(IO, "Content-Length: %i", (int)content_length);
|
||||
} else {
|
||||
std::string key_str(key, key_len);
|
||||
std::transform(key_str.begin(), key_str.end(), key_str.begin(), tolower);
|
||||
other[key_str] = buffer;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void RequestHeader::ParseHeaders(net::InputSink *sink) {
|
||||
|
@ -12,39 +12,39 @@ class InputSink;
|
||||
namespace http {
|
||||
|
||||
class RequestHeader {
|
||||
public:
|
||||
RequestHeader();
|
||||
~RequestHeader();
|
||||
// Public variables since it doesn't make sense
|
||||
// to bother with accessors for all these.
|
||||
int status;
|
||||
// Intentional misspelling.
|
||||
char *referer;
|
||||
char *user_agent;
|
||||
char *resource;
|
||||
char *params;
|
||||
int content_length;
|
||||
std::unordered_map<std::string, std::string> other;
|
||||
enum RequestType {
|
||||
SIMPLE, FULL,
|
||||
};
|
||||
RequestType type;
|
||||
enum Method {
|
||||
GET,
|
||||
HEAD,
|
||||
POST,
|
||||
UNSUPPORTED,
|
||||
};
|
||||
Method method;
|
||||
bool ok;
|
||||
void ParseHeaders(net::InputSink *sink);
|
||||
bool GetParamValue(const char *param_name, std::string *value) const;
|
||||
bool GetOther(const char *name, std::string *value) const;
|
||||
private:
|
||||
int ParseHttpHeader(const char *buffer);
|
||||
bool first_header_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(RequestHeader);
|
||||
public:
|
||||
RequestHeader();
|
||||
~RequestHeader();
|
||||
// Public variables since it doesn't make sense
|
||||
// to bother with accessors for all these.
|
||||
int status = 100;
|
||||
// Intentional misspelling.
|
||||
char *referer = nullptr;
|
||||
char *user_agent = nullptr;
|
||||
char *resource = nullptr;
|
||||
char *params = nullptr;
|
||||
int content_length = -1;
|
||||
std::unordered_map<std::string, std::string> other;
|
||||
enum RequestType {
|
||||
SIMPLE, FULL,
|
||||
};
|
||||
RequestType type;
|
||||
enum Method {
|
||||
GET,
|
||||
HEAD,
|
||||
POST,
|
||||
UNSUPPORTED,
|
||||
};
|
||||
Method method = UNSUPPORTED;
|
||||
bool ok = false;
|
||||
void ParseHeaders(net::InputSink *sink);
|
||||
bool GetParamValue(const char *param_name, std::string *value) const;
|
||||
bool GetOther(const char *name, std::string *value) const;
|
||||
private:
|
||||
int ParseHttpHeader(const char *buffer);
|
||||
bool first_header_ = true;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(RequestHeader);
|
||||
};
|
||||
|
||||
} // namespace http
|
||||
|
@ -58,7 +58,7 @@ namespace http {
|
||||
const char *const DEFAULT_MIME_TYPE = "text/html; charset=utf-8";
|
||||
|
||||
Request::Request(int fd)
|
||||
: fd_(fd) {
|
||||
: fd_(fd) {
|
||||
in_ = new net::InputSink(fd);
|
||||
out_ = new net::OutputSink(fd);
|
||||
header_.ParseHeaders(in_);
|
||||
@ -137,7 +137,7 @@ void Request::Close() {
|
||||
}
|
||||
|
||||
Server::Server(NewThreadExecutor *executor)
|
||||
: port_(0), executor_(executor) {
|
||||
: executor_(executor) {
|
||||
RegisterHandler("/", std::bind(&Server::HandleListing, this, std::placeholders::_1));
|
||||
SetFallbackHandler(std::bind(&Server::Handle404, this, std::placeholders::_1));
|
||||
}
|
||||
|
@ -24,40 +24,40 @@ class OutputSink;
|
||||
namespace http {
|
||||
|
||||
class Request {
|
||||
public:
|
||||
Request(int fd);
|
||||
~Request();
|
||||
public:
|
||||
Request(int fd);
|
||||
~Request();
|
||||
|
||||
const char *resource() const {
|
||||
return header_.resource;
|
||||
}
|
||||
const char *resource() const {
|
||||
return header_.resource;
|
||||
}
|
||||
|
||||
RequestHeader::Method Method() const {
|
||||
return header_.method;
|
||||
}
|
||||
RequestHeader::Method Method() const {
|
||||
return header_.method;
|
||||
}
|
||||
|
||||
bool GetParamValue(const char *param_name, std::string *value) const {
|
||||
return header_.GetParamValue(param_name, value);
|
||||
}
|
||||
// Use lowercase.
|
||||
bool GetHeader(const char *name, std::string *value) const {
|
||||
return header_.GetOther(name, value);
|
||||
}
|
||||
bool GetParamValue(const char *param_name, std::string *value) const {
|
||||
return header_.GetParamValue(param_name, value);
|
||||
}
|
||||
// Use lowercase.
|
||||
bool GetHeader(const char *name, std::string *value) const {
|
||||
return header_.GetOther(name, value);
|
||||
}
|
||||
|
||||
net::InputSink *In() const { return in_; }
|
||||
net::OutputSink *Out() const { return out_; }
|
||||
net::InputSink *In() const { return in_; }
|
||||
net::OutputSink *Out() const { return out_; }
|
||||
|
||||
// TODO: Remove, in favor of PartialWrite and friends.
|
||||
int fd() const { return fd_; }
|
||||
// TODO: Remove, in favor of PartialWrite and friends.
|
||||
int fd() const { return fd_; }
|
||||
|
||||
void WritePartial() const;
|
||||
void Write();
|
||||
void Close();
|
||||
void WritePartial() const;
|
||||
void Write();
|
||||
void Close();
|
||||
|
||||
bool IsOK() const { return fd_ > 0; }
|
||||
bool IsOK() const { return fd_ > 0; }
|
||||
|
||||
// If size is negative, no Content-Length: line is written.
|
||||
void WriteHttpResponseHeader(const char *ver, int status, int64_t size = -1, const char *mimeType = nullptr, const char *otherHeaders = nullptr) const;
|
||||
// If size is negative, no Content-Length: line is written.
|
||||
void WriteHttpResponseHeader(const char *ver, int status, int64_t size = -1, const char *mimeType = nullptr, const char *otherHeaders = nullptr) const;
|
||||
|
||||
private:
|
||||
net::InputSink *in_;
|
||||
@ -112,7 +112,7 @@ private:
|
||||
void Handle404(const Request &request);
|
||||
|
||||
int listener_;
|
||||
int port_;
|
||||
int port_ = 0;
|
||||
|
||||
UrlHandlerMap handlers_;
|
||||
UrlHandlerFunc fallback_;
|
||||
|
@ -47,12 +47,12 @@ static const size_t OUT_PRESSURE = 65536;
|
||||
static inline std::string TrimString(const std::string &s) {
|
||||
auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) {
|
||||
// isspace() expects 0 - 255, so convert any sign-extended value.
|
||||
return std::isspace(c & 0xFF);
|
||||
});
|
||||
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c){
|
||||
return std::isspace(c & 0xFF);
|
||||
}).base();
|
||||
return wsback > wsfront ? std::string(wsfront, wsback) : std::string();
|
||||
return std::isspace(c & 0xFF);
|
||||
});
|
||||
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c){
|
||||
return std::isspace(c & 0xFF);
|
||||
}).base();
|
||||
return wsback > wsfront ? std::string(wsfront, wsback) : std::string();
|
||||
}
|
||||
|
||||
static bool ListContainsNoCase(const std::string &list, const std::string value) {
|
||||
|
Loading…
Reference in New Issue
Block a user