1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Benjamin Smedberg <benjamin@smedbergs.us>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsAutoPtr.h"
40 : #include "nsJARProtocolHandler.h"
41 : #include "nsIIOService.h"
42 : #include "nsCRT.h"
43 : #include "nsIComponentManager.h"
44 : #include "nsIServiceManager.h"
45 : #include "nsJARURI.h"
46 : #include "nsIURL.h"
47 : #include "nsJARChannel.h"
48 : #include "nsXPIDLString.h"
49 : #include "nsString.h"
50 : #include "nsNetCID.h"
51 : #include "nsIMIMEService.h"
52 : #include "nsMimeTypes.h"
53 :
54 : static NS_DEFINE_CID(kZipReaderCacheCID, NS_ZIPREADERCACHE_CID);
55 :
56 : #define NS_JAR_CACHE_SIZE 32
57 :
58 : //-----------------------------------------------------------------------------
59 :
60 : nsJARProtocolHandler *gJarHandler = nsnull;
61 :
62 1419 : nsJARProtocolHandler::nsJARProtocolHandler()
63 : {
64 1419 : }
65 :
66 2838 : nsJARProtocolHandler::~nsJARProtocolHandler()
67 : {
68 5676 : }
69 :
70 : nsresult
71 1419 : nsJARProtocolHandler::Init()
72 : {
73 : nsresult rv;
74 :
75 1419 : mJARCache = do_CreateInstance(kZipReaderCacheCID, &rv);
76 1419 : if (NS_FAILED(rv)) return rv;
77 :
78 1419 : rv = mJARCache->Init(NS_JAR_CACHE_SIZE);
79 1419 : return rv;
80 : }
81 :
82 : nsIMIMEService *
83 0 : nsJARProtocolHandler::MimeService()
84 : {
85 0 : if (!mMimeService)
86 0 : mMimeService = do_GetService("@mozilla.org/mime;1");
87 :
88 0 : return mMimeService.get();
89 : }
90 :
91 58367 : NS_IMPL_THREADSAFE_ISUPPORTS3(nsJARProtocolHandler,
92 : nsIJARProtocolHandler,
93 : nsIProtocolHandler,
94 : nsISupportsWeakReference)
95 :
96 : nsJARProtocolHandler*
97 1419 : nsJARProtocolHandler::GetSingleton()
98 : {
99 1419 : if (!gJarHandler) {
100 1419 : gJarHandler = new nsJARProtocolHandler();
101 1419 : if (!gJarHandler)
102 0 : return nsnull;
103 :
104 1419 : NS_ADDREF(gJarHandler);
105 1419 : nsresult rv = gJarHandler->Init();
106 1419 : if (NS_FAILED(rv)) {
107 0 : NS_RELEASE(gJarHandler);
108 0 : return nsnull;
109 : }
110 : }
111 1419 : NS_ADDREF(gJarHandler);
112 1419 : return gJarHandler;
113 : }
114 :
115 : NS_IMETHODIMP
116 0 : nsJARProtocolHandler::GetJARCache(nsIZipReaderCache* *result)
117 : {
118 0 : *result = mJARCache;
119 0 : NS_ADDREF(*result);
120 0 : return NS_OK;
121 : }
122 :
123 : ////////////////////////////////////////////////////////////////////////////////
124 : // nsIProtocolHandler methods:
125 :
126 : NS_IMETHODIMP
127 0 : nsJARProtocolHandler::GetScheme(nsACString &result)
128 : {
129 0 : result.AssignLiteral("jar");
130 0 : return NS_OK;
131 : }
132 :
133 : NS_IMETHODIMP
134 0 : nsJARProtocolHandler::GetDefaultPort(PRInt32 *result)
135 : {
136 0 : *result = -1; // no port for JAR: URLs
137 0 : return NS_OK;
138 : }
139 :
140 : NS_IMETHODIMP
141 416 : nsJARProtocolHandler::GetProtocolFlags(PRUint32 *result)
142 : {
143 : // URI_LOADABLE_BY_ANYONE, since it's our inner URI that will matter
144 : // anyway.
145 416 : *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE;
146 : /* Although jar uris have their own concept of relative urls
147 : it is very different from the standard behaviour, so we
148 : have to say norelative here! */
149 416 : return NS_OK;
150 : }
151 :
152 : NS_IMETHODIMP
153 2144 : nsJARProtocolHandler::NewURI(const nsACString &aSpec,
154 : const char *aCharset,
155 : nsIURI *aBaseURI,
156 : nsIURI **result)
157 : {
158 2144 : nsresult rv = NS_OK;
159 :
160 4288 : nsRefPtr<nsJARURI> jarURI = new nsJARURI();
161 2144 : if (!jarURI)
162 0 : return NS_ERROR_OUT_OF_MEMORY;
163 :
164 2144 : rv = jarURI->Init(aCharset);
165 2144 : NS_ENSURE_SUCCESS(rv, rv);
166 :
167 2144 : rv = jarURI->SetSpecWithBase(aSpec, aBaseURI);
168 2144 : if (NS_FAILED(rv))
169 50 : return rv;
170 :
171 2094 : NS_ADDREF(*result = jarURI);
172 2094 : return rv;
173 : }
174 :
175 : NS_IMETHODIMP
176 399 : nsJARProtocolHandler::NewChannel(nsIURI *uri, nsIChannel **result)
177 : {
178 399 : nsJARChannel *chan = new nsJARChannel();
179 399 : if (!chan)
180 0 : return NS_ERROR_OUT_OF_MEMORY;
181 399 : NS_ADDREF(chan);
182 :
183 399 : nsresult rv = chan->Init(uri);
184 399 : if (NS_FAILED(rv)) {
185 0 : NS_RELEASE(chan);
186 0 : return rv;
187 : }
188 :
189 399 : *result = chan;
190 399 : return NS_OK;
191 : }
192 :
193 :
194 : NS_IMETHODIMP
195 0 : nsJARProtocolHandler::AllowPort(PRInt32 port, const char *scheme, bool *_retval)
196 : {
197 : // don't override anything.
198 0 : *_retval = false;
199 0 : return NS_OK;
200 : }
201 :
202 : ////////////////////////////////////////////////////////////////////////////////
|