Buffet utilities updated for GCD device registration
Updated some utility functions to provid the functionality needed
for implementing GCD device registration services.
DataEncoding - typedefed key-value pairs as WebParamList to simplify
the usage of WebParamsEncode/WebParamsDecode functions
http::Request - Added a helper function to add a list of HTTP headers
to the request at once.
http::curl::Transport - added a lot of debug logging information to
help debugging HTTP communications between Buffet and external
servers. Also fixed sending data with custom request such as PATCH.
Finally, response headers used to include trailing new line chars.
http::TransportInterface - provided a typedef for the list of
HTTP request headers
http_utils - Switched from using char const* to std::string const&
in URLs for ease of use.
Added generic SendRequest().
Added the ability to add custom request headers when using PostXXX()
Added helper for sending PATCH request with JSON objects.
Changed ParseJsonResponse() to expect JSON object (dictionary)
instead of generic value as the server response. This simplifies the
common usage. Also enabled this function to parse error responses
as well.
BUG=chromium:363348
TEST=unit tests passed.
Change-Id: Ieb407731d6664feb0370bbaeeda16df8f6b7c5d1
Reviewed-on: https://chromium-review.googlesource.com/194856
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/http_utils.h b/buffet/http_utils.h
index 0fff847..724f118 100644
--- a/buffet/http_utils.h
+++ b/buffet/http_utils.h
@@ -7,11 +7,16 @@
#include "buffet/http_request.h"
-namespace base { class Value; }
+namespace base {
+ class Value;
+ class DictionaryValue;
+} // namespace base
namespace chromeos {
namespace http {
+typedef std::vector<std::pair<std::string, std::string>> FormFieldList;
+
////////////////////////////////////////////////////////////////////////////////
// The following are simple utility helper functions for common HTTP operations
// that use http::Request object behind the scenes and set it up accordingly.
@@ -20,29 +25,48 @@
// directly.
////////////////////////////////////////////////////////////////////////////////
+// Performs a generic HTTP request with binary data. Success status,
+// returned data and additional information (such as returned HTTP headers)
+// can be obtained from the returned Response object.
+// If data MIME type is not specified, "application/octet-stream" is assumed.
+std::unique_ptr<Response> SendRequest(char const* method,
+ std::string const& url,
+ void const* data,
+ size_t data_size,
+ char const* mime_type,
+ HeaderList const& headers);
+
// Performs a simple GET request and returns the data as a string.
-std::string GetAsString(char const* url);
+std::string GetAsString(std::string const& url);
// Performs a GET request. Success status, returned data and additional
// information (such as returned HTTP headers) can be obtained from
// the returned Response object.
-std::unique_ptr<Response> Get(char const* url);
+std::unique_ptr<Response> Get(std::string const& url);
// Performs a HEAD request. Success status and additional
// information (such as returned HTTP headers) can be obtained from
// the returned Response object.
-std::unique_ptr<Response> Head(char const* url);
+std::unique_ptr<Response> Head(std::string const& url);
// Performs a POST request with binary data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object.
// If data MIME type is not specified, "application/octet-stream" is assumed
-std::unique_ptr<Response> PostBinary(char const* url,
+std::unique_ptr<Response> PostBinary(std::string const& url,
void const* data,
size_t data_size,
- char const* mime_type);
+ char const* mime_type,
+ HeaderList const& headers);
-inline std::unique_ptr<Response> PostBinary(char const* url,
+inline std::unique_ptr<Response> PostBinary(std::string const& url,
+ void const* data,
+ size_t data_size,
+ char const* mime_type) {
+ return PostBinary(url, data, data_size, mime_type, HeaderList());
+}
+
+inline std::unique_ptr<Response> PostBinary(std::string const& url,
void const* data,
size_t data_size) {
return PostBinary(url, data, data_size, nullptr);
@@ -52,26 +76,67 @@
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object.
// If data MIME type is not specified, "application/x-www-form-urlencoded"
-// is assumed
-std::unique_ptr<Response> PostText(char const* url,
+// is assumed.
+std::unique_ptr<Response> PostText(std::string const& url,
char const* data,
- char const* mime_type);
+ char const* mime_type,
+ HeaderList const& headers);
-inline std::unique_ptr<Response> PostText(char const* url, char const* data) {
+inline std::unique_ptr<Response> PostText(std::string const& url,
+ char const* data,
+ char const* mime_type) {
+ return PostText(url, data, mime_type, HeaderList());
+}
+
+inline std::unique_ptr<Response> PostText(std::string const& url,
+ char const* data) {
return PostText(url, data, nullptr);
}
+// Performs a POST request with form data. Success status, returned data
+// and additional information (such as returned HTTP headers) can be obtained
+// from the returned Response object. The form data is a list of key/value
+// pairs. The data is posed as "application/x-www-form-urlencoded".
+std::unique_ptr<Response> PostFormData(std::string const& url,
+ FormFieldList const& data,
+ HeaderList const& headers);
+
+inline std::unique_ptr<Response> PostFormData(std::string const& url,
+ FormFieldList const& data) {
+ return PostFormData(url, data, HeaderList());
+}
+
// Performs a POST request with JSON data. Success status, returned data
// and additional information (such as returned HTTP headers) can be obtained
// from the returned Response object. If a JSON response is expected,
// use ParseJsonResponse() method on the returned Response object.
-std::unique_ptr<Response> PostJson(char const* url, base::Value const* json);
+std::unique_ptr<Response> PostJson(std::string const& url,
+ base::Value const* json,
+ HeaderList const& headers);
+
+inline std::unique_ptr<Response> PostJson(std::string const& url,
+ base::Value const* json) {
+ return PostJson(url, json, HeaderList());
+}
+
+// Performs a PATCH request with JSON data. Success status, returned data
+// and additional information (such as returned HTTP headers) can be obtained
+// from the returned Response object. If a JSON response is expected,
+// use ParseJsonResponse() method on the returned Response object.
+std::unique_ptr<Response> PatchJson(std::string const& url,
+ base::Value const* json,
+ HeaderList const& headers);
+
+inline std::unique_ptr<Response> PatchJson(std::string const& url,
+ base::Value const* json) {
+ return PatchJson(url, json, HeaderList());
+}
// Given an http::Response object, parse the body data into Json object.
// Returns null if failed. Optional |error_message| can be passed in to
// get the extended error information as to why the parse failed.
-std::unique_ptr<base::Value> ParseJsonResponse(Response const* response,
- std::string* error_message);
+std::unique_ptr<base::DictionaryValue> ParseJsonResponse(
+ Response const* response, int* status_code, std::string* error_message);
} // namespace http
} // namespace chromeos