summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgrunell@chromium.org <grunell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-16 12:59:18 +0000
committergrunell@chromium.org <grunell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-16 12:59:18 +0000
commit811d279ae31f8f2eaae114c0d9eeeeeb986247e1 (patch)
treeed558b6f97899302ccedc1f3aef3810e5854a08d
parent3a94b2b997fecdba63a9cbb02865e7b66738bfa5 (diff)
downloadchromium_src-811d279ae31f8f2eaae114c0d9eeeeeb986247e1.zip
chromium_src-811d279ae31f8f2eaae114c0d9eeeeeb986247e1.tar.gz
chromium_src-811d279ae31f8f2eaae114c0d9eeeeeb986247e1.tar.bz2
Move AddMultipartValueForUpload to net/base/mime_util.h/cc.
Moved from cloud print since it is used from several places. This was triggered by https://chromiumcodereview.appspot.com/14329020/ were there also is a discussion about this move. BUG=229829 R=scottbyer@chromium.org, sky@chromium.org, wtc@chromium.org Review URL: https://codereview.chromium.org/15076008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@200521 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/chrome_to_mobile_service.cc11
-rw-r--r--chrome/common/cloud_print/cloud_print_helpers.cc27
-rw-r--r--chrome/common/cloud_print/cloud_print_helpers.h7
-rw-r--r--chrome/service/cloud_print/cloud_print_connector.cc26
-rw-r--r--chrome/service/cloud_print/printer_job_handler.cc18
-rw-r--r--net/base/mime_util.cc25
-rw-r--r--net/base/mime_util.h12
7 files changed, 68 insertions, 58 deletions
diff --git a/chrome/browser/chrome_to_mobile_service.cc b/chrome/browser/chrome_to_mobile_service.cc
index 1f9e2f9..dfb42f7 100644
--- a/chrome/browser/chrome_to_mobile_service.cc
+++ b/chrome/browser/chrome_to_mobile_service.cc
@@ -45,6 +45,7 @@
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "net/base/escape.h"
#include "net/base/load_flags.h"
+#include "net/base/mime_util.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
@@ -137,13 +138,13 @@ std::string GetContentType(ChromeToMobileService::JobType type) {
"multipart/related" : "text/plain";
}
-// Utility function to call cloud_print::AddMultipartValueForUpload.
+// Utility function to call net::AddMultipartValueForUpload.
void AddValue(const std::string& value_name,
const std::string& value,
const std::string& mime_boundary,
std::string* post_data) {
- cloud_print::AddMultipartValueForUpload(value_name, value, mime_boundary,
- std::string(), post_data);
+ net::AddMultipartValueForUpload(value_name, value, mime_boundary,
+ std::string(), post_data);
}
// Append the Chrome To Mobile client query parameter, used by cloud print.
@@ -583,10 +584,10 @@ void ChromeToMobileService::SendJobRequest(base::WeakPtr<Observer> observer,
AddValue("contentType", GetContentType(data.type), bound, &post);
// Add the snapshot or use dummy content to workaround a URL submission error.
- cloud_print::AddMultipartValueForUpload("content",
+ net::AddMultipartValueForUpload("content",
data.snapshot_content.empty() ? "content" : data.snapshot_content,
bound, "text/mhtml", &post);
- post.append("--" + bound + "--\r\n");
+ net::AddMultipartFinalDelimiterForUpload(bound, &post);
LogMetric(data.type == SNAPSHOT ? SENDING_SNAPSHOT : SENDING_URL);
net::URLFetcher* request = net::URLFetcher::Create(
diff --git a/chrome/common/cloud_print/cloud_print_helpers.cc b/chrome/common/cloud_print/cloud_print_helpers.cc
index 28096c8..3305c5b 100644
--- a/chrome/common/cloud_print/cloud_print_helpers.cc
+++ b/chrome/common/cloud_print/cloud_print_helpers.cc
@@ -15,6 +15,7 @@
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "googleurl/src/gurl.h"
+#include "net/base/mime_util.h"
namespace cloud_print {
@@ -188,26 +189,6 @@ scoped_ptr<base::DictionaryValue> ParseResponseJSON(
return response_dict.Pass();
}
-void AddMultipartValueForUpload(const std::string& value_name,
- const std::string& value,
- const std::string& mime_boundary,
- const std::string& content_type,
- std::string* post_data) {
- DCHECK(post_data);
- // First line is the boundary
- post_data->append("--" + mime_boundary + "\r\n");
- // Next line is the Content-disposition
- post_data->append(base::StringPrintf("Content-Disposition: form-data; "
- "name=\"%s\"\r\n", value_name.c_str()));
- if (!content_type.empty()) {
- // If Content-type is specified, the next line is that
- post_data->append(base::StringPrintf("Content-Type: %s\r\n",
- content_type.c_str()));
- }
- // Leave an empty line and append the value.
- post_data->append(base::StringPrintf("\r\n%s\r\n", value.c_str()));
-}
-
std::string GetMultipartMimeType(const std::string& mime_boundary) {
return std::string("multipart/form-data; boundary=") + mime_boundary;
}
@@ -241,14 +222,14 @@ std::string GetPostDataForPrinterTags(
// All our tags have a special prefix to identify them as such.
std::string msg = base::StringPrintf("%s%s=%s",
proxy_tag_prefix.c_str(), it->first.c_str(), it->second.c_str());
- AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
+ net::AddMultipartValueForUpload(kPrinterTagValue, msg, mime_boundary,
std::string(), &post_data);
}
std::string tags_hash_msg = base::StringPrintf("%s=%s",
tags_hash_tag_name.c_str(),
HashPrinterTags(printer_tags_prepared).c_str());
- AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg, mime_boundary,
- std::string(), &post_data);
+ net::AddMultipartValueForUpload(kPrinterTagValue, tags_hash_msg,
+ mime_boundary, std::string(), &post_data);
return post_data;
}
diff --git a/chrome/common/cloud_print/cloud_print_helpers.h b/chrome/common/cloud_print/cloud_print_helpers.h
index 71bd9b6..23a9f8f 100644
--- a/chrome/common/cloud_print/cloud_print_helpers.h
+++ b/chrome/common/cloud_print/cloud_print_helpers.h
@@ -63,13 +63,6 @@ scoped_ptr<base::DictionaryValue> ParseResponseJSON(
const std::string& response_data,
bool* succeeded);
-// Prepares one value as part of a multi-part upload request.
-void AddMultipartValueForUpload(const std::string& value_name,
- const std::string& value,
- const std::string& mime_boundary,
- const std::string& content_type,
- std::string* post_data);
-
// Returns the MIME type of multipart with |mime_boundary|.
std::string GetMultipartMimeType(const std::string& mime_boundary);
diff --git a/chrome/service/cloud_print/cloud_print_connector.cc b/chrome/service/cloud_print/cloud_print_connector.cc
index 0e2e0ec..54016b6 100644
--- a/chrome/service/cloud_print/cloud_print_connector.cc
+++ b/chrome/service/cloud_print/cloud_print_connector.cc
@@ -17,6 +17,7 @@
#include "chrome/common/cloud_print/cloud_print_helpers.h"
#include "chrome/service/cloud_print/cloud_print_helpers.h"
#include "grit/generated_resources.h"
+#include "net/base/mime_util.h"
#include "ui/base/l10n/l10n_util.h"
namespace cloud_print {
@@ -313,10 +314,9 @@ void CloudPrintConnector::ReportUserMessage(const std::string& message_id,
CreateMimeBoundaryForUpload(&mime_boundary);
GURL url = GetUrlForUserMessage(settings_.server_url(), message_id);
std::string post_data;
- AddMultipartValueForUpload(kMessageTextValue, failure_msg, mime_boundary,
- std::string(), &post_data);
- // Terminate the request body
- post_data.append("--" + mime_boundary + "--\r\n");
+ net::AddMultipartValueForUpload(kMessageTextValue, failure_msg, mime_boundary,
+ std::string(), &post_data);
+ net::AddMultipartFinalDelimiterForUpload(mime_boundary, &post_data);
std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary;
user_message_request_ = CloudPrintURLFetcher::Create();
@@ -527,30 +527,28 @@ void CloudPrintConnector::OnReceivePrinterCaps(
CreateMimeBoundaryForUpload(&mime_boundary);
std::string post_data;
- AddMultipartValueForUpload(kProxyIdValue,
+ net::AddMultipartValueForUpload(kProxyIdValue,
settings_.proxy_id(), mime_boundary, std::string(), &post_data);
- AddMultipartValueForUpload(kPrinterNameValue,
+ net::AddMultipartValueForUpload(kPrinterNameValue,
info.printer_name, mime_boundary, std::string(), &post_data);
- AddMultipartValueForUpload(kPrinterDescValue,
+ net::AddMultipartValueForUpload(kPrinterDescValue,
info.printer_description, mime_boundary, std::string(), &post_data);
- AddMultipartValueForUpload(kPrinterStatusValue,
+ net::AddMultipartValueForUpload(kPrinterStatusValue,
base::StringPrintf("%d", info.printer_status),
mime_boundary, std::string(), &post_data);
post_data += GetPostDataForPrinterInfo(info, mime_boundary);
- AddMultipartValueForUpload(kPrinterCapsValue,
+ net::AddMultipartValueForUpload(kPrinterCapsValue,
caps_and_defaults.printer_capabilities, mime_boundary,
caps_and_defaults.caps_mime_type, &post_data);
- AddMultipartValueForUpload(kPrinterDefaultsValue,
+ net::AddMultipartValueForUpload(kPrinterDefaultsValue,
caps_and_defaults.printer_defaults, mime_boundary,
caps_and_defaults.defaults_mime_type, &post_data);
// Send a hash of the printer capabilities to the server. We will use this
// later to check if the capabilities have changed
- AddMultipartValueForUpload(kPrinterCapsHashValue,
+ net::AddMultipartValueForUpload(kPrinterCapsHashValue,
base::MD5String(caps_and_defaults.printer_capabilities),
mime_boundary, std::string(), &post_data);
-
- // Terminate the request body
- post_data.append("--" + mime_boundary + "--\r\n");
+ net::AddMultipartFinalDelimiterForUpload(mime_boundary, &post_data);
std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary;
diff --git a/chrome/service/cloud_print/printer_job_handler.cc b/chrome/service/cloud_print/printer_job_handler.cc
index 9a7b37e..74542a18 100644
--- a/chrome/service/cloud_print/printer_job_handler.cc
+++ b/chrome/service/cloud_print/printer_job_handler.cc
@@ -18,6 +18,7 @@
#include "chrome/service/cloud_print/job_status_updater.h"
#include "googleurl/src/gurl.h"
#include "grit/generated_resources.h"
+#include "net/base/mime_util.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "printing/backend/print_backend.h"
@@ -596,13 +597,13 @@ void PrinterJobHandler::OnReceivePrinterCaps(
// Hashes don't match, we need to upload new capabilities (the defaults
// go for free along with the capabilities)
printer_info_cloud_.caps_hash = caps_hash;
- AddMultipartValueForUpload(kPrinterCapsValue,
+ net::AddMultipartValueForUpload(kPrinterCapsValue,
caps_and_defaults.printer_capabilities, mime_boundary,
caps_and_defaults.caps_mime_type, &post_data);
- AddMultipartValueForUpload(kPrinterDefaultsValue,
+ net::AddMultipartValueForUpload(kPrinterDefaultsValue,
caps_and_defaults.printer_defaults, mime_boundary,
caps_and_defaults.defaults_mime_type, &post_data);
- AddMultipartValueForUpload(kPrinterCapsHashValue,
+ net::AddMultipartValueForUpload(kPrinterCapsHashValue,
caps_hash, mime_boundary, std::string(), &post_data);
}
} else {
@@ -617,28 +618,27 @@ void PrinterJobHandler::OnReceivePrinterCaps(
// Remove all the existing proxy tags.
std::string cp_tag_wildcard(kCloudPrintServiceProxyTagPrefix);
cp_tag_wildcard += ".*";
- AddMultipartValueForUpload(kPrinterRemoveTagValue,
+ net::AddMultipartValueForUpload(kPrinterRemoveTagValue,
cp_tag_wildcard, mime_boundary, std::string(), &post_data);
}
if (printer_info.printer_name != printer_info_.printer_name) {
- AddMultipartValueForUpload(kPrinterNameValue,
+ net::AddMultipartValueForUpload(kPrinterNameValue,
printer_info.printer_name, mime_boundary, std::string(), &post_data);
}
if (printer_info.printer_description != printer_info_.printer_description) {
- AddMultipartValueForUpload(kPrinterDescValue,
+ net::AddMultipartValueForUpload(kPrinterDescValue,
printer_info.printer_description, mime_boundary,
std::string(), &post_data);
}
if (printer_info.printer_status != printer_info_.printer_status) {
- AddMultipartValueForUpload(kPrinterStatusValue,
+ net::AddMultipartValueForUpload(kPrinterStatusValue,
base::StringPrintf("%d", printer_info.printer_status), mime_boundary,
std::string(), &post_data);
}
printer_info_ = printer_info;
if (!post_data.empty()) {
- // Terminate the request body
- post_data.append("--" + mime_boundary + "--\r\n");
+ net::AddMultipartFinalDelimiterForUpload(mime_boundary, &post_data);
std::string mime_type("multipart/form-data; boundary=");
mime_type += mime_boundary;
SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse);
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index 67c7242..3212b5c 100644
--- a/net/base/mime_util.cc
+++ b/net/base/mime_util.cc
@@ -987,4 +987,29 @@ bool IsSupportedCertificateMimeType(const std::string& mime_type) {
return file_type != CERTIFICATE_MIME_TYPE_UNKNOWN;
}
+void AddMultipartValueForUpload(const std::string& value_name,
+ const std::string& value,
+ const std::string& mime_boundary,
+ const std::string& content_type,
+ std::string* post_data) {
+ DCHECK(post_data);
+ // First line is the boundary.
+ post_data->append("--" + mime_boundary + "\r\n");
+ // Next line is the Content-disposition.
+ post_data->append("Content-Disposition: form-data; name=\"" +
+ value_name + "\"\r\n");
+ if (!content_type.empty()) {
+ // If Content-type is specified, the next line is that.
+ post_data->append("Content-Type: " + content_type + "\r\n");
+ }
+ // Leave an empty line and append the value.
+ post_data->append("\r\n" + value + "\r\n");
+}
+
+void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
+ std::string* post_data) {
+ DCHECK(post_data);
+ post_data->append("--" + mime_boundary + "--\r\n");
+}
+
} // namespace net
diff --git a/net/base/mime_util.h b/net/base/mime_util.h
index d2638cb..fc0999b 100644
--- a/net/base/mime_util.h
+++ b/net/base/mime_util.h
@@ -119,6 +119,18 @@ enum CertificateMimeType {
NET_EXPORT CertificateMimeType GetCertificateMimeTypeForMimeType(
const std::string& mime_type);
+// Prepares one value as part of a multi-part upload request.
+NET_EXPORT void AddMultipartValueForUpload(const std::string& value_name,
+ const std::string& value,
+ const std::string& mime_boundary,
+ const std::string& content_type,
+ std::string* post_data);
+
+// Adds the final delimiter to a multi-part upload request.
+NET_EXPORT void AddMultipartFinalDelimiterForUpload(
+ const std::string& mime_boundary,
+ std::string* post_data);
+
} // namespace net
#endif // NET_BASE_MIME_UTIL_H__