1 // Copyright 2015 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 #include "net/url_request/certificate_report_sender.h"
7 #include "base/stl_util.h"
8 #include "net/base/elements_upload_data_stream.h"
9 #include "net/base/load_flags.h"
10 #include "net/base/request_priority.h"
11 #include "net/base/upload_bytes_element_reader.h"
12 #include "net/url_request/url_request_context.h"
13 #include "net/url_request/url_request_status.h"
17 CertificateReportSender::CertificateReportSender(
18 URLRequestContext
* request_context
,
19 CookiesPreference cookies_preference
)
20 : request_context_(request_context
),
21 cookies_preference_(cookies_preference
) {}
23 CertificateReportSender::~CertificateReportSender() {
24 STLDeleteElements(&inflight_requests_
);
27 void CertificateReportSender::Send(const GURL
& report_uri
,
28 const std::string
& report
) {
29 scoped_ptr
<URLRequest
> url_request
=
30 CreateURLRequest(request_context_
, report_uri
);
31 url_request
->set_method("POST");
33 scoped_ptr
<UploadElementReader
> reader(
34 UploadOwnedBytesElementReader::CreateWithString(report
));
35 url_request
->set_upload(
36 ElementsUploadDataStream::CreateWithReader(reader
.Pass(), 0));
38 URLRequest
* raw_url_request
= url_request
.get();
39 inflight_requests_
.insert(url_request
.release());
40 raw_url_request
->Start();
43 void CertificateReportSender::OnResponseStarted(URLRequest
* request
) {
44 // TODO(estark): call a callback so that the caller can print a
45 // warning on failure.
46 RequestComplete(request
);
49 void CertificateReportSender::OnReadCompleted(URLRequest
* request
,
54 scoped_ptr
<URLRequest
> CertificateReportSender::CreateURLRequest(
55 URLRequestContext
* context
,
56 const GURL
& report_uri
) {
57 scoped_ptr
<URLRequest
> request
=
58 context
->CreateRequest(report_uri
, DEFAULT_PRIORITY
, this);
60 LOAD_BYPASS_CACHE
| LOAD_DISABLE_CACHE
| LOAD_DO_NOT_SEND_AUTH_DATA
;
61 if (cookies_preference_
!= SEND_COOKIES
) {
63 load_flags
| LOAD_DO_NOT_SEND_COOKIES
| LOAD_DO_NOT_SAVE_COOKIES
;
65 request
->SetLoadFlags(load_flags
);
66 return request
.Pass();
69 void CertificateReportSender::RequestComplete(URLRequest
* request
) {
70 std::set
<URLRequest
*>::iterator i
= inflight_requests_
.find(request
);
71 CHECK(i
!= inflight_requests_
.end());
72 scoped_ptr
<URLRequest
> url_request(*i
);
73 inflight_requests_
.erase(i
);