mirror of
https://github.com/hrydgard/ppsspp.git
synced 2026-11-19 20:34:26 +01:00
Merge pull request #89 from unknownbrackets/http
Support non-chunked HTTP responses
This commit is contained in:
commit
0c0b023058
4 files changed
+65
-15
No files matched your search
+21
-7
@@ -36,6 +36,12 @@ void Buffer::Append(const char *str) {
|
||||
memcpy(dest, str, len);
|
||||
}
|
||||
|
||||
void Buffer::Append(const Buffer &other) {
|
||||
size_t len = other.size();
|
||||
char *dest = Append(len);
|
||||
memcpy(dest, &other.data_[0], len);
|
||||
}
|
||||
|
||||
void Buffer::AppendValue(int value) {
|
||||
char buf[16];
|
||||
// This is slow.
|
||||
@@ -44,8 +50,11 @@ void Buffer::AppendValue(int value) {
|
||||
}
|
||||
|
||||
void Buffer::Take(size_t length, std::string *dest) {
|
||||
CHECK_LE(length, data_.size());
|
||||
dest->resize(length);
|
||||
if (length > data_.size()) {
|
||||
ELOG("Truncating length in Buffer::Take()");
|
||||
length = data_.size();
|
||||
}
|
||||
dest->resize(length);
|
||||
if (length > 0) {
|
||||
memcpy(&(*dest)[0], &data_[0], length);
|
||||
data_.erase(data_.begin(), data_.begin() + length);
|
||||
@@ -64,7 +73,11 @@ int Buffer::TakeLineCRLF(std::string *dest) {
|
||||
}
|
||||
|
||||
void Buffer::Skip(size_t length) {
|
||||
data_.erase(data_.begin(), data_.begin() + length);
|
||||
if (length > data_.size()) {
|
||||
ELOG("Truncating length in Buffer::Skip()");
|
||||
length = data_.size();
|
||||
}
|
||||
data_.erase(data_.begin(), data_.begin() + length);
|
||||
}
|
||||
|
||||
int Buffer::SkipLineCRLF() {
|
||||
@@ -87,18 +100,19 @@ int Buffer::OffsetToAfterNextCRLF() {
|
||||
}
|
||||
|
||||
void Buffer::Printf(const char *fmt, ...) {
|
||||
char buffer[512];
|
||||
char buffer[2048];
|
||||
va_list vl;
|
||||
va_start(vl, fmt);
|
||||
ssize_t retval = vsnprintf(buffer, sizeof(buffer), fmt, vl);
|
||||
if (retval >= (ssize_t)sizeof(buffer)) {
|
||||
// Output was truncated. TODO: Do something.
|
||||
FLOG("Buffer::Printf truncated output");
|
||||
ELOG("Buffer::Printf truncated output");
|
||||
}
|
||||
if (retval < 0) {
|
||||
ELOG("Buffer::Printf failed");
|
||||
}
|
||||
CHECK_GE(retval, 0);
|
||||
va_end(vl);
|
||||
char *ptr = Append(retval);
|
||||
CHECK(ptr);
|
||||
memcpy(ptr, buffer, retval);
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -19,9 +19,10 @@ class Buffer {
|
||||
char *Append(ssize_t length);
|
||||
char *Append(size_t length) { return Append((ssize_t)length); }
|
||||
|
||||
// These work pretty much like you'd expect.
|
||||
// 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 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.
|
||||
|
||||
+39
-5
@@ -66,9 +66,12 @@ bool Connection::Resolve(const char *host, int port) {
|
||||
}
|
||||
|
||||
bool Connection::Connect() {
|
||||
CHECK_GE(port_, 0);
|
||||
if (port_ <= 0) {
|
||||
ELOG("Bad port");
|
||||
return false;
|
||||
}
|
||||
sock_ = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if ((intptr_t)sock_ < 0) {
|
||||
if ((intptr_t)sock_ == -1) {
|
||||
ELOG("Bad socket");
|
||||
return false;
|
||||
}
|
||||
@@ -89,6 +92,10 @@ bool Connection::Connect() {
|
||||
sleep(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Let's not leak this socket.
|
||||
closesocket(sock_);
|
||||
sock_ = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -165,25 +172,52 @@ int Client::GET(const char *resource, Buffer *output) {
|
||||
|
||||
std::string line;
|
||||
readbuf.TakeLineCRLF(&line);
|
||||
int code = atoi(&line[line.find(" ") + 1]);
|
||||
|
||||
int code;
|
||||
size_t code_pos = line.find(' ');
|
||||
if (code_pos != line.npos) {
|
||||
code_pos = line.find_first_not_of(' ', code_pos);
|
||||
}
|
||||
if (code_pos != line.npos) {
|
||||
code = atoi(&line[code_pos]);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool gzip = false;
|
||||
bool chunked = false;
|
||||
int contentLength = 0;
|
||||
while (true) {
|
||||
int sz = readbuf.TakeLineCRLF(&line);
|
||||
if (!sz)
|
||||
break;
|
||||
// TODO: Case folding.
|
||||
if (startsWith(line, "Content-Length:")) {
|
||||
contentLength = atoi(&line[16]);
|
||||
size_t size_pos = line.find_first_of(' ');
|
||||
if (size_pos != line.npos) {
|
||||
size_pos = line.find_first_not_of(' ', size_pos);
|
||||
}
|
||||
if (size_pos != line.npos) {
|
||||
contentLength = atoi(&line[size_pos]);
|
||||
chunked = false;
|
||||
}
|
||||
} else if (startsWith(line, "Content-Encoding:")) {
|
||||
if (line.find("gzip") != std::string::npos) {
|
||||
gzip = true;
|
||||
}
|
||||
} else if (startsWith(line, "Transfer-Encoding:")) {
|
||||
if (line.find("chunked") != std::string::npos) {
|
||||
chunked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output now contains the rest of the reply. Dechunk it.
|
||||
DeChunk(&readbuf, output);
|
||||
if (chunked) {
|
||||
DeChunk(&readbuf, output);
|
||||
} else {
|
||||
output->Append(readbuf);
|
||||
}
|
||||
|
||||
// If it's gzipped, we decompress it and put it back in the buffer.
|
||||
if (gzip) {
|
||||
|
||||
+2
-1
@@ -121,7 +121,8 @@ bool DNSResolve(const std::string &host, const std::string &service, addrinfo **
|
||||
|
||||
void DNSResolveFree(addrinfo *res)
|
||||
{
|
||||
freeaddrinfo(res);
|
||||
if (res != NULL)
|
||||
freeaddrinfo(res);
|
||||
}
|
||||
|
||||
int inet_pton(int af, const char* src, void* dst)
|
||||
|
||||
Reference in new issue
Block a user