summaryrefslogtreecommitdiffstats
path: root/net/url_request/url_request_data_job.cc
blob: b4c7a67d62f554f9a480fd59a9376af8cd2b98a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Simple implementation of a data: protocol handler.

#include "net/url_request/url_request_data_job.h"

#include "net/base/data_url.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "url/gurl.h"

namespace net {

int URLRequestDataJob::BuildResponse(const GURL& url,
                                     std::string* mime_type,
                                     std::string* charset,
                                     std::string* data,
                                     HttpResponseHeaders* headers) {
  if (!DataURL::Parse(url, mime_type, charset, data))
    return ERR_INVALID_URL;

  // |mime_type| set by DataURL::Parse() is guaranteed to be in
  //     token "/" token
  // form. |charset| is also guaranteed to be a token.

  DCHECK(!mime_type->empty());
  DCHECK(!charset->empty());

  if (headers) {
    headers->ReplaceStatusLine("HTTP/1.1 200 OK");
    // "charset" in the Content-Type header is specified explicitly to follow
    // the "token" ABNF in the HTTP spec. When DataURL::Parse() call is
    // successful, it's guaranteed that the string in |charset| follows the
    // "token" ABNF.
    std::string content_type_header =
        "Content-Type: " + *mime_type + ";charset=" + *charset;
    headers->AddHeader(content_type_header);
    headers->AddHeader("Access-Control-Allow-Origin: *");
  }

  return OK;
}

URLRequestDataJob::URLRequestDataJob(
    URLRequest* request, NetworkDelegate* network_delegate)
    : URLRequestSimpleJob(request, network_delegate) {
}

int URLRequestDataJob::GetData(std::string* mime_type,
                               std::string* charset,
                               std::string* data,
                               const CompletionCallback& callback) const {
  // Check if data URL is valid. If not, don't bother to try to extract data.
  // Otherwise, parse the data from the data URL.
  const GURL& url = request_->url();
  if (!url.is_valid())
    return ERR_INVALID_URL;

  // TODO(tyoshino): Get the headers and export via
  // URLRequestJob::GetResponseInfo().
  return BuildResponse(url, mime_type, charset, data, NULL);
}

URLRequestDataJob::~URLRequestDataJob() {
}

}  // namespace net