Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/viewsource/nsViewSourceHandler.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* vim:set ts=4 sw=4 sts=4 et: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsViewSourceHandler.h"
8
#include "nsViewSourceChannel.h"
9
#include "nsNetUtil.h"
10
#include "nsSimpleNestedURI.h"
11
12
0
#define VIEW_SOURCE "view-source"
13
14
0
#define DEFAULT_FLAGS (URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD | URI_NON_PERSISTABLE)
15
16
namespace mozilla {
17
namespace net {
18
19
////////////////////////////////////////////////////////////////////////////////
20
21
NS_IMPL_ISUPPORTS(nsViewSourceHandler, nsIProtocolHandler,
22
    nsIProtocolHandlerWithDynamicFlags)
23
24
////////////////////////////////////////////////////////////////////////////////
25
// nsIProtocolHandler methods:
26
27
NS_IMETHODIMP
28
nsViewSourceHandler::GetScheme(nsACString &result)
29
0
{
30
0
    result.AssignLiteral(VIEW_SOURCE);
31
0
    return NS_OK;
32
0
}
33
34
NS_IMETHODIMP
35
nsViewSourceHandler::GetDefaultPort(int32_t *result)
36
0
{
37
0
    *result = -1;
38
0
    return NS_OK;
39
0
}
40
41
NS_IMETHODIMP
42
nsViewSourceHandler::GetProtocolFlags(uint32_t *result)
43
0
{
44
0
    *result = DEFAULT_FLAGS;
45
0
    return NS_OK;
46
0
}
47
48
NS_IMETHODIMP
49
nsViewSourceHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* result)
50
0
{
51
0
    *result = DEFAULT_FLAGS;
52
0
    nsCOMPtr<nsINestedURI> nestedURI(do_QueryInterface(aURI));
53
0
    if (!nestedURI) {
54
0
        return NS_OK;
55
0
    }
56
0
57
0
    nsCOMPtr<nsIURI> innerURI;
58
0
    nestedURI->GetInnerURI(getter_AddRefs(innerURI));
59
0
    nsCOMPtr<nsINetUtil> netUtil = do_GetNetUtil();
60
0
    bool isLoadable = false;
61
0
    nsresult rv = netUtil->ProtocolHasFlags(innerURI, URI_LOADABLE_BY_ANYONE, &isLoadable);
62
0
    NS_ENSURE_SUCCESS(rv, rv);
63
0
    if (isLoadable) {
64
0
        *result |= URI_LOADABLE_BY_EXTENSIONS;
65
0
    }
66
0
    return NS_OK;
67
0
}
68
69
NS_IMETHODIMP
70
nsViewSourceHandler::NewURI(const nsACString &aSpec,
71
                            const char *aCharset,
72
                            nsIURI *aBaseURI,
73
                            nsIURI **aResult)
74
0
{
75
0
    *aResult = nullptr;
76
0
77
0
    // Extract inner URL and normalize to ASCII.  This is done to properly
78
0
    // support IDN in cases like "view-source:http://www.szalagavató.hu/"
79
0
80
0
    int32_t colon = aSpec.FindChar(':');
81
0
    if (colon == kNotFound)
82
0
        return NS_ERROR_MALFORMED_URI;
83
0
84
0
    nsCOMPtr<nsIURI> innerURI;
85
0
    nsresult rv = NS_NewURI(getter_AddRefs(innerURI),
86
0
                            Substring(aSpec, colon + 1), aCharset, aBaseURI);
87
0
    if (NS_FAILED(rv))
88
0
        return rv;
89
0
90
0
    nsAutoCString asciiSpec;
91
0
    rv = innerURI->GetAsciiSpec(asciiSpec);
92
0
    if (NS_FAILED(rv))
93
0
        return rv;
94
0
95
0
    // put back our scheme and construct a simple-uri wrapper
96
0
97
0
    asciiSpec.Insert(VIEW_SOURCE ":", 0);
98
0
99
0
    nsCOMPtr<nsIURI> uri;
100
0
    rv = NS_MutateURI(new nsSimpleNestedURI::Mutator())
101
0
           .Apply(NS_MutatorMethod(&nsINestedURIMutator::Init, innerURI))
102
0
           .SetSpec(asciiSpec)
103
0
           .Finalize(uri);
104
0
    if (NS_FAILED(rv)) {
105
0
        return rv;
106
0
    }
107
0
108
0
    uri.swap(*aResult);
109
0
    return rv;
110
0
}
111
112
NS_IMETHODIMP
113
nsViewSourceHandler::NewChannel2(nsIURI* uri,
114
                                 nsILoadInfo* aLoadInfo,
115
                                 nsIChannel** result)
116
0
{
117
0
    NS_ENSURE_ARG_POINTER(uri);
118
0
    nsViewSourceChannel *channel = new nsViewSourceChannel();
119
0
    if (!channel)
120
0
        return NS_ERROR_OUT_OF_MEMORY;
121
0
    NS_ADDREF(channel);
122
0
123
0
    nsresult rv = channel->Init(uri);
124
0
    if (NS_FAILED(rv)) {
125
0
        NS_RELEASE(channel);
126
0
        return rv;
127
0
    }
128
0
129
0
    // set the loadInfo on the new channel
130
0
    rv = channel->SetLoadInfo(aLoadInfo);
131
0
    if (NS_FAILED(rv)) {
132
0
        NS_RELEASE(channel);
133
0
        return rv;
134
0
    }
135
0
136
0
    *result = static_cast<nsIViewSourceChannel*>(channel);
137
0
    return NS_OK;
138
0
}
139
140
NS_IMETHODIMP
141
nsViewSourceHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
142
0
{
143
0
    return NewChannel2(uri, nullptr, result);
144
0
}
145
146
nsresult
147
nsViewSourceHandler::NewSrcdocChannel(nsIURI *aURI,
148
                                      nsIURI *aBaseURI,
149
                                      const nsAString &aSrcdoc,
150
                                      nsILoadInfo* aLoadInfo,
151
                                      nsIChannel** outChannel)
152
0
{
153
0
    NS_ENSURE_ARG_POINTER(aURI);
154
0
    RefPtr<nsViewSourceChannel> channel = new nsViewSourceChannel();
155
0
156
0
    nsresult rv = channel->InitSrcdoc(aURI, aBaseURI, aSrcdoc, aLoadInfo);
157
0
    if (NS_FAILED(rv)) {
158
0
        return rv;
159
0
    }
160
0
161
0
    *outChannel = static_cast<nsIViewSourceChannel*>(channel.forget().take());
162
0
    return NS_OK;
163
0
}
164
165
NS_IMETHODIMP
166
nsViewSourceHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
167
0
{
168
0
    // don't override anything.
169
0
    *_retval = false;
170
0
    return NS_OK;
171
0
}
172
173
nsViewSourceHandler::nsViewSourceHandler()
174
0
{
175
0
    gInstance = this;
176
0
}
177
178
nsViewSourceHandler::~nsViewSourceHandler()
179
0
{
180
0
    gInstance = nullptr;
181
0
}
182
183
nsViewSourceHandler* nsViewSourceHandler::gInstance = nullptr;
184
185
nsViewSourceHandler*
186
nsViewSourceHandler::GetInstance()
187
0
{
188
0
    return gInstance;
189
0
}
190
191
} // namespace net
192
} // namespace mozilla