Skip to content

Commit

Permalink
[NaCl SDK] Refactor FakeURLRequestInfoResource, FakeURLResponseInfoRe…
Browse files Browse the repository at this point in the history
…source, and GetHeaderValue

Move FakeURLRequestInfoResource, FakeURLResponseInfoResource, and
GetHeaderValue from fake_pepper_interface_url_loader.cc to
fake_util.{cc,h} to share the code with the future code.

Modify slightly on the implementation of GetHeaderValue.

depot_tools/clang-format was run so the formatting of code is accepted
in the code review by policy. It was run in all the {.cc,.h} files.

test_sdk.py was run. Unit tests passed on all platforms.
Not any compiler errors were from fake_pepper_interface_url_loader*
and fake_util*.

CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_nacl_sdk;master.tryserver.chromium.mac:mac_nacl_sdk;master.tryserver.chromium.win:win_nacl_sdk

Review-Url: https://codereview.chromium.org/2485203005
Cr-Commit-Position: refs/heads/master@{#432085}
  • Loading branch information
chanpatorikku authored and Commit bot committed Nov 15, 2016
1 parent 24314a8 commit ca44a7a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "fake_ppapi/fake_pepper_interface_url_loader.h"

#include <string.h>
#include <strings.h>

#include <algorithm>
#include <sstream>
Expand All @@ -17,38 +16,6 @@

namespace {

bool GetHeaderValue(const std::string& headers,
const std::string& key,
std::string* out_value) {
out_value->clear();

size_t offset = 0;
while (offset != std::string::npos) {
// Find the next colon; this separates the key from the value.
size_t colon = headers.find(':', offset);
if (colon == std::string::npos)
return false;

// Find the newline; this separates the value from the next header.
size_t newline = headers.find('\n', offset);
if (strncasecmp(key.c_str(), &headers.data()[offset], key.size()) != 0) {
// Key doesn't match, skip to next header.
offset = newline;
continue;
}

// Key matches, extract value. First, skip leading spaces.
size_t nonspace = headers.find_first_not_of(' ', colon + 1);
if (nonspace == std::string::npos)
return false;

out_value->assign(headers, nonspace, newline - nonspace);
return true;
}

return false;
}

class FakeInstanceResource : public FakeResource {
public:
FakeInstanceResource() : server_template(NULL) {}
Expand Down Expand Up @@ -83,26 +50,6 @@ class FakeURLLoaderResource : public FakeResource {
off_t read_end;
};

class FakeURLRequestInfoResource : public FakeResource {
public:
FakeURLRequestInfoResource() {}
static const char* classname() { return "FakeURLRequestInfoResource"; }

std::string url;
std::string method;
std::string headers;
};

class FakeURLResponseInfoResource : public FakeResource {
public:
FakeURLResponseInfoResource() : status_code(0) {}
static const char* classname() { return "FakeURLResponseInfoResource"; }

int status_code;
std::string url;
std::string headers;
};

void HandleContentLength(FakeURLLoaderResource* loader,
FakeURLResponseInfoResource* response,
FakeURLLoaderEntity* entity) {
Expand Down
36 changes: 36 additions & 0 deletions native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

#include "fake_ppapi/fake_util.h"

#include <strings.h>

#include <string>

#include <ppapi/c/pp_completion_callback.h>
#include <ppapi/c/pp_errors.h>

Expand All @@ -24,3 +28,35 @@ int32_t RunCompletionCallback(PP_CompletionCallback* callback, int32_t result) {
}
return result;
}

bool GetHeaderValue(const std::string& headers,
const std::string& key,
std::string* out_value) {
out_value->clear();

size_t offset = 0;
while (offset != std::string::npos) {
// Find the next colon; this separates the key from the value.
size_t colon = headers.find(':', offset);
if (colon == std::string::npos)
return false;

// Find the newline; this separates the value from the next header.
size_t newline = headers.find('\n', offset);
if (strncasecmp(key.c_str(), &headers.data()[offset], key.size()) != 0) {
// Key doesn't match, skip to next header.
offset = newline + 1;
continue;
}

// Key matches, extract value. First, skip leading spaces.
size_t nonspace = headers.find_first_not_of(' ', colon + 1);
if (nonspace == std::string::npos)
return false;

out_value->assign(headers, nonspace, newline - nonspace);
return true;
}

return false;
}
23 changes: 23 additions & 0 deletions native_client_sdk/src/tests/nacl_io_test/fake_ppapi/fake_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ class FakeHtml5FsResource : public FakeResource {
FakeFilesystem* filesystem_template; // Weak reference.
};

class FakeURLRequestInfoResource : public FakeResource {
public:
FakeURLRequestInfoResource() {}
static const char* classname() { return "FakeURLRequestInfoResource"; }

std::string url;
std::string method;
std::string headers;
};

class FakeURLResponseInfoResource : public FakeResource {
public:
FakeURLResponseInfoResource() : status_code(0) {}
static const char* classname() { return "FakeURLResponseInfoResource"; }

int status_code;
std::string url;
std::string headers;
};

int32_t RunCompletionCallback(PP_CompletionCallback* callback, int32_t result);
bool GetHeaderValue(const std::string& headers,
const std::string& key,
std::string* out_value);

#endif // LIBRARIES_NACL_IO_TEST_FAKE_UTIL_H_

0 comments on commit ca44a7a

Please sign in to comment.