diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 05:56:20 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-15 05:56:20 +0000 |
commit | 9731edec74129f5c96c150e38a36671d6c08636f (patch) | |
tree | 54d2515ad6ccbf3febfbae686050ba1d67442478 /webkit/glue | |
parent | 2cc1f382e7257e045e529b0aa70ecb3c650bd066 (diff) | |
download | chromium_src-9731edec74129f5c96c150e38a36671d6c08636f.zip chromium_src-9731edec74129f5c96c150e38a36671d6c08636f.tar.gz chromium_src-9731edec74129f5c96c150e38a36671d6c08636f.tar.bz2 |
Add support for POST requests and custom request headers.
I decided against having URLRequestInfo contain a WebURLRequest object.
Instead, there is a ToWebURLRequest method that takes a WebFrame.
Fixed a bug where we wouldn't report EOF properly in the case where
didFinishLoading was called before ReadResponseBody.
R=jam
BUG=47222
TEST=ppapi/tests/test_url_loader.cc
Review URL: http://codereview.chromium.org/2893016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52450 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue')
-rw-r--r-- | webkit/glue/plugins/pepper_file_ref.cc | 1 | ||||
-rw-r--r-- | webkit/glue/plugins/pepper_file_ref.h | 8 | ||||
-rw-r--r-- | webkit/glue/plugins/pepper_url_loader.cc | 18 | ||||
-rw-r--r-- | webkit/glue/plugins/pepper_url_loader.h | 1 | ||||
-rw-r--r-- | webkit/glue/plugins/pepper_url_request_info.cc | 94 | ||||
-rw-r--r-- | webkit/glue/plugins/pepper_url_request_info.h | 47 |
6 files changed, 143 insertions, 26 deletions
diff --git a/webkit/glue/plugins/pepper_file_ref.cc b/webkit/glue/plugins/pepper_file_ref.cc index 8ea8c4f..63563a1 100644 --- a/webkit/glue/plugins/pepper_file_ref.cc +++ b/webkit/glue/plugins/pepper_file_ref.cc @@ -129,6 +129,7 @@ FileRef::FileRef(PluginModule* module, fs_type_(file_system_type), path_(validated_path), origin_(origin) { + // TODO(darin): Need to initialize system_path_. } FileRef::~FileRef() { diff --git a/webkit/glue/plugins/pepper_file_ref.h b/webkit/glue/plugins/pepper_file_ref.h index b43cba5..34e4c3e 100644 --- a/webkit/glue/plugins/pepper_file_ref.h +++ b/webkit/glue/plugins/pepper_file_ref.h @@ -7,6 +7,7 @@ #include <string> +#include "base/file_path.h" #include "third_party/ppapi/c/ppb_file_ref.h" #include "webkit/glue/plugins/pepper_resource.h" @@ -34,9 +35,16 @@ class FileRef : public Resource { scoped_refptr<FileRef> GetParent(); PP_FileSystemType file_system_type() const { return fs_type_; } + + // Returns the virtual path (i.e., the path that the pepper plugin sees) + // corresponding to this file. const std::string& path() const { return path_; } + // Returns the system path corresponding to this file. + const FilePath& system_path() const { return system_path_; } + private: + FilePath system_path_; PP_FileSystemType fs_type_; std::string path_; // UTF-8 encoded. std::string origin_; diff --git a/webkit/glue/plugins/pepper_url_loader.cc b/webkit/glue/plugins/pepper_url_loader.cc index 0cf03a0..c20d8e8 100644 --- a/webkit/glue/plugins/pepper_url_loader.cc +++ b/webkit/glue/plugins/pepper_url_loader.cc @@ -14,6 +14,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" +#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" #include "webkit/glue/plugins/pepper_plugin_instance.h" #include "webkit/glue/plugins/pepper_url_request_info.h" #include "webkit/glue/plugins/pepper_url_response_info.h" @@ -153,7 +154,8 @@ URLLoader::URLLoader(PluginInstance* instance) bytes_received_(0), total_bytes_to_be_received_(0), user_buffer_(NULL), - user_buffer_size_(0) { + user_buffer_size_(0), + done_(false) { } URLLoader::~URLLoader() { @@ -173,14 +175,10 @@ int32_t URLLoader::Open(URLRequestInfo* request, if (!callback.func) return PP_ERROR_BADARGUMENT; - WebURLRequest web_request(request->web_request()); - WebFrame* frame = instance_->container()->element().document().frame(); if (!frame) return PP_ERROR_FAILED; - web_request.setURL( - frame->document().completeURL(WebString::fromUTF8(request->url()))); - frame->setReferrerForRequest(web_request, WebURL()); // Use default. + WebURLRequest web_request(request->ToWebURLRequest(frame)); frame->dispatchWillSendRequest(web_request); loader_.reset(WebKit::webKitClient()->createURLLoader()); @@ -218,6 +216,12 @@ int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, if (!buffer_.empty()) return FillUserBuffer(); + if (done_) { + user_buffer_ = NULL; + user_buffer_size_ = 0; + return 0; + } + pending_callback_ = callback; return PP_ERROR_WOULDBLOCK; } @@ -258,10 +262,12 @@ void URLLoader::didReceiveData(WebURLLoader* loader, } void URLLoader::didFinishLoading(WebURLLoader* loader) { + done_ = true; RunCallback(PP_OK); } void URLLoader::didFail(WebURLLoader* loader, const WebURLError& error) { + done_ = true; // TODO(darin): Provide more detailed error information. RunCallback(PP_ERROR_FAILED); } diff --git a/webkit/glue/plugins/pepper_url_loader.h b/webkit/glue/plugins/pepper_url_loader.h index 6e29f8d..088f220 100644 --- a/webkit/glue/plugins/pepper_url_loader.h +++ b/webkit/glue/plugins/pepper_url_loader.h @@ -81,6 +81,7 @@ class URLLoader : public Resource, public WebKit::WebURLLoaderClient { int64_t total_bytes_to_be_received_; char* user_buffer_; size_t user_buffer_size_; + bool done_; }; } // namespace pepper diff --git a/webkit/glue/plugins/pepper_url_request_info.cc b/webkit/glue/plugins/pepper_url_request_info.cc index 7813136..94ce1f4 100644 --- a/webkit/glue/plugins/pepper_url_request_info.cc +++ b/webkit/glue/plugins/pepper_url_request_info.cc @@ -5,20 +5,48 @@ #include "webkit/glue/plugins/pepper_url_request_info.h" #include "base/logging.h" +#include "base/string_util.h" #include "googleurl/src/gurl.h" +#include "net/http/http_util.h" #include "third_party/ppapi/c/pp_var.h" +#include "third_party/WebKit/WebKit/chromium/public/WebData.h" +#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" +#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" +#include "third_party/WebKit/WebKit/chromium/public/WebHTTPBody.h" #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" +#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" #include "webkit/glue/plugins/pepper_file_ref.h" #include "webkit/glue/plugins/pepper_plugin_module.h" #include "webkit/glue/plugins/pepper_string.h" #include "webkit/glue/plugins/pepper_var.h" +#include "webkit/glue/webkit_glue.h" +using WebKit::WebData; +using WebKit::WebFileInfo; +using WebKit::WebHTTPBody; using WebKit::WebString; +using WebKit::WebFrame; +using WebKit::WebURL; +using WebKit::WebURLRequest; namespace pepper { namespace { +// If any of these request headers are specified, they will not be sent. +// TODO(darin): Add more based on security considerations? +const char* const kIgnoredRequestHeaders[] = { + "content-length" +}; + +bool IsIgnoredRequestHeader(const std::string& name) { + for (size_t i = 0; i < arraysize(kIgnoredRequestHeaders); ++i) { + if (LowerCaseEqualsASCII(name, kIgnoredRequestHeaders[i])) + return true; + } + return false; +} + PP_Resource Create(PP_Module module_id) { PluginModule* module = PluginModule::FromPPModule(module_id); if (!module) @@ -96,7 +124,6 @@ const PPB_URLRequestInfo ppb_urlrequestinfo = { URLRequestInfo::URLRequestInfo(PluginModule* module) : Resource(module) { - web_request_.initialize(); } URLRequestInfo::~URLRequestInfo() { @@ -118,33 +145,76 @@ bool URLRequestInfo::SetStringProperty(PP_URLRequestProperty property, // TODO(darin): Validate input. Perhaps at a different layer? switch (property) { case PP_URLREQUESTPROPERTY_URL: - // Keep the url in a string instead of a URL object because it might not - // be complete yet. - url_ = value; + url_ = value; // NOTE: This may be a relative URL. return true; case PP_URLREQUESTPROPERTY_METHOD: - web_request_.setHTTPMethod(WebString::fromUTF8(value)); + method_ = value; return true; case PP_URLREQUESTPROPERTY_HEADERS: - // TODO(darin): Support extra request headers - NOTIMPLEMENTED(); - return false; + headers_ = value; + return true; default: return false; } } bool URLRequestInfo::AppendDataToBody(const std::string& data) { - NOTIMPLEMENTED(); // TODO(darin): Implement me! - return false; + body_.push_back(BodyItem(data)); + return true; } bool URLRequestInfo::AppendFileToBody(FileRef* file_ref, int64_t start_offset, int64_t number_of_bytes, PP_Time expected_last_modified_time) { - NOTIMPLEMENTED(); // TODO(darin): Implement me! - return false; + body_.push_back(BodyItem(file_ref, + start_offset, + number_of_bytes, + expected_last_modified_time)); + return true; +} + +WebURLRequest URLRequestInfo::ToWebURLRequest(WebFrame* frame) const { + WebURLRequest web_request; + web_request.initialize(); + web_request.setURL(frame->document().completeURL(WebString::fromUTF8(url_))); + + if (!method_.empty()) + web_request.setHTTPMethod(WebString::fromUTF8(method_)); + + if (!headers_.empty()) { + net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), "\n"); + while (it.GetNext()) { + if (!IsIgnoredRequestHeader(it.name())) { + web_request.addHTTPHeaderField( + WebString::fromUTF8(it.name()), + WebString::fromUTF8(it.values())); + } + } + } + + if (!body_.empty()) { + WebHTTPBody http_body; + http_body.initialize(); + for (size_t i = 0; i < body_.size(); ++i) { + if (body_[i].file_ref) { + WebFileInfo file_info; + file_info.modificationTime = body_[i].expected_last_modified_time; + http_body.appendFileRange( + webkit_glue::FilePathToWebString(body_[i].file_ref->system_path()), + body_[i].start_offset, + body_[i].number_of_bytes, + file_info); + } else { + DCHECK(!body_[i].data.empty()); + http_body.appendData(WebData(body_[i].data)); + } + } + web_request.setHTTPBody(http_body); + } + + frame->setReferrerForRequest(web_request, WebURL()); // Use default. + return web_request; } } // namespace pepper diff --git a/webkit/glue/plugins/pepper_url_request_info.h b/webkit/glue/plugins/pepper_url_request_info.h index f988bf7..ef1452c 100644 --- a/webkit/glue/plugins/pepper_url_request_info.h +++ b/webkit/glue/plugins/pepper_url_request_info.h @@ -6,14 +6,19 @@ #define WEBKIT_GLUE_PLUGINS_PEPPER_URL_REQUEST_INFO_H_ #include <string> +#include <vector> +#include "base/ref_counted.h" #include "third_party/ppapi/c/ppb_url_request_info.h" -#include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" +#include "webkit/glue/plugins/pepper_file_ref.h" #include "webkit/glue/plugins/pepper_resource.h" -namespace pepper { +namespace WebKit { +class WebFrame; +class WebURLRequest; +} -class FileRef; +namespace pepper { class URLRequestInfo : public Resource { public: @@ -37,14 +42,40 @@ class URLRequestInfo : public Resource { int64_t number_of_bytes, PP_Time expected_last_modified_time); - const WebKit::WebURLRequest& web_request() const { - return web_request_; - } - std::string url() const { return url_; } + WebKit::WebURLRequest ToWebURLRequest(WebKit::WebFrame* frame) const; private: + struct BodyItem { + BodyItem(const std::string& data) + : data(data), + start_offset(0), + number_of_bytes(-1), + expected_last_modified_time(0.0) { + } + + BodyItem(FileRef* file_ref, + int64_t start_offset, + int64_t number_of_bytes, + PP_Time expected_last_modified_time) + : file_ref(file_ref), + start_offset(start_offset), + number_of_bytes(number_of_bytes), + expected_last_modified_time(expected_last_modified_time) { + } + + std::string data; + scoped_refptr<FileRef> file_ref; + int64_t start_offset; + int64_t number_of_bytes; + PP_Time expected_last_modified_time; + }; + + typedef std::vector<BodyItem> Body; + std::string url_; - WebKit::WebURLRequest web_request_; + std::string method_; + std::string headers_; + Body body_; }; } // namespace pepper |