Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_data_job.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Simple implementation of a data: protocol handler.
6
7 #include "net/url_request/url_request_data_job.h"
8
9 #include "base/profiler/scoped_tracker.h"
10 #include "net/base/data_url.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_response_headers.h"
13 #include "url/gurl.h"
14
15 namespace net {
16
17 int URLRequestDataJob::BuildResponse(const GURL& url,
18                                      std::string* mime_type,
19                                      std::string* charset,
20                                      std::string* data,
21                                      HttpResponseHeaders* headers) {
22   if (!net::DataURL::Parse(url, mime_type, charset, data))
23     return net::ERR_INVALID_URL;
24
25   // |mime_type| set by net::DataURL::Parse() is guaranteed to be in
26   //     token "/" token
27   // form. |charset| is also guaranteed to be a token.
28
29   DCHECK(!mime_type->empty());
30   DCHECK(!charset->empty());
31
32   if (headers) {
33     headers->ReplaceStatusLine("HTTP/1.1 200 OK");
34     // "charset" in the Content-Type header is specified explicitly to follow
35     // the "token" ABNF in the HTTP spec. When DataURL::Parse() call is
36     // successful, it's guaranteed that the string in |charset| follows the
37     // "token" ABNF.
38     std::string content_type_header =
39         "Content-Type: " + *mime_type + ";charset=" + *charset;
40     headers->AddHeader(content_type_header);
41     headers->AddHeader("Access-Control-Allow-Origin: *");
42   }
43
44   return net::OK;
45 }
46
47 URLRequestDataJob::URLRequestDataJob(
48     URLRequest* request, NetworkDelegate* network_delegate)
49     : URLRequestSimpleJob(request, network_delegate) {
50 }
51
52 int URLRequestDataJob::GetData(std::string* mime_type,
53                                std::string* charset,
54                                std::string* data,
55                                const CompletionCallback& callback) const {
56   // TODO(vadimt): Remove ScopedTracker below once crbug.com/422489 is fixed.
57   tracked_objects::ScopedTracker tracking_profile(
58       FROM_HERE_WITH_EXPLICIT_FUNCTION("422489 URLRequestDataJob::GetData"));
59
60   // Check if data URL is valid. If not, don't bother to try to extract data.
61   // Otherwise, parse the data from the data URL.
62   const GURL& url = request_->url();
63   if (!url.is_valid())
64     return ERR_INVALID_URL;
65
66   // TODO(tyoshino): Get the headers and export via
67   // URLRequestJob::GetResponseInfo().
68   return BuildResponse(url, mime_type, charset, data, NULL);
69 }
70
71 URLRequestDataJob::~URLRequestDataJob() {
72 }
73
74 }  // namespace net