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.
5 // Simple implementation of a data: protocol handler.
7 #include "net/url_request/url_request_data_job.h"
9 #include "net/base/data_url.h"
10 #include "net/base/net_errors.h"
11 #include "net/http/http_response_headers.h"
16 int URLRequestDataJob::BuildResponse(const GURL
& url
,
17 std::string
* mime_type
,
20 HttpResponseHeaders
* headers
) {
21 if (!DataURL::Parse(url
, mime_type
, charset
, data
))
22 return ERR_INVALID_URL
;
24 // |mime_type| set by DataURL::Parse() is guaranteed to be in
26 // form. |charset| is also guaranteed to be a token.
28 DCHECK(!mime_type
->empty());
29 DCHECK(!charset
->empty());
32 headers
->ReplaceStatusLine("HTTP/1.1 200 OK");
33 // "charset" in the Content-Type header is specified explicitly to follow
34 // the "token" ABNF in the HTTP spec. When DataURL::Parse() call is
35 // successful, it's guaranteed that the string in |charset| follows the
37 std::string content_type_header
=
38 "Content-Type: " + *mime_type
+ ";charset=" + *charset
;
39 headers
->AddHeader(content_type_header
);
40 headers
->AddHeader("Access-Control-Allow-Origin: *");
46 URLRequestDataJob::URLRequestDataJob(
47 URLRequest
* request
, NetworkDelegate
* network_delegate
)
48 : URLRequestSimpleJob(request
, network_delegate
) {
51 int URLRequestDataJob::GetData(std::string
* mime_type
,
54 const CompletionCallback
& callback
) const {
55 // Check if data URL is valid. If not, don't bother to try to extract data.
56 // Otherwise, parse the data from the data URL.
57 const GURL
& url
= request_
->url();
59 return ERR_INVALID_URL
;
61 // TODO(tyoshino): Get the headers and export via
62 // URLRequestJob::GetResponseInfo().
63 return BuildResponse(url
, mime_type
, charset
, data
, NULL
);
66 URLRequestDataJob::~URLRequestDataJob() {