Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #include <stdio.h> |
michael@0 | 6 | #include <signal.h> |
michael@0 | 7 | |
michael@0 | 8 | #ifdef WIN32 |
michael@0 | 9 | #include <windows.h> |
michael@0 | 10 | #endif |
michael@0 | 11 | |
michael@0 | 12 | #include "nspr.h" |
michael@0 | 13 | #include "nscore.h" |
michael@0 | 14 | #include "nsISocketTransportService.h" |
michael@0 | 15 | #include "nsIEventQueueService.h" |
michael@0 | 16 | #include "nsIServiceManager.h" |
michael@0 | 17 | #include "nsITransport.h" |
michael@0 | 18 | #include "nsIRequest.h" |
michael@0 | 19 | #include "nsIStreamProvider.h" |
michael@0 | 20 | #include "nsIStreamListener.h" |
michael@0 | 21 | #include "nsIPipe.h" |
michael@0 | 22 | #include "nsIOutputStream.h" |
michael@0 | 23 | #include "nsIInputStream.h" |
michael@0 | 24 | #include "nsCRT.h" |
michael@0 | 25 | #include "nsCOMPtr.h" |
michael@0 | 26 | #include "nsIByteArrayInputStream.h" |
michael@0 | 27 | |
michael@0 | 28 | #if defined(PR_LOGGING) |
michael@0 | 29 | static PRLogModuleInfo *gTestSocketIOLog; |
michael@0 | 30 | #define LOG(args) PR_LOG(gTestSocketIOLog, PR_LOG_DEBUG, args) |
michael@0 | 31 | #else |
michael@0 | 32 | #define LOG(args) |
michael@0 | 33 | #endif |
michael@0 | 34 | |
michael@0 | 35 | static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); |
michael@0 | 36 | static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); |
michael@0 | 37 | |
michael@0 | 38 | static PRTime gElapsedTime; |
michael@0 | 39 | static int gKeepRunning = 1; |
michael@0 | 40 | static nsIEventQueue* gEventQ = nullptr; |
michael@0 | 41 | |
michael@0 | 42 | // |
michael@0 | 43 | //---------------------------------------------------------------------------- |
michael@0 | 44 | // Test Listener |
michael@0 | 45 | //---------------------------------------------------------------------------- |
michael@0 | 46 | // |
michael@0 | 47 | |
michael@0 | 48 | class TestListener : public nsIStreamListener |
michael@0 | 49 | { |
michael@0 | 50 | public: |
michael@0 | 51 | TestListener() {} |
michael@0 | 52 | virtual ~TestListener() {} |
michael@0 | 53 | |
michael@0 | 54 | NS_DECL_ISUPPORTS |
michael@0 | 55 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 56 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | NS_IMPL_ISUPPORTS(TestListener, |
michael@0 | 60 | nsIRequestObserver, |
michael@0 | 61 | nsIStreamListener); |
michael@0 | 62 | |
michael@0 | 63 | NS_IMETHODIMP |
michael@0 | 64 | TestListener::OnStartRequest(nsIRequest* request, nsISupports* context) |
michael@0 | 65 | { |
michael@0 | 66 | LOG(("TestListener::OnStartRequest\n")); |
michael@0 | 67 | return NS_OK; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | NS_IMETHODIMP |
michael@0 | 71 | TestListener::OnDataAvailable(nsIRequest* request, |
michael@0 | 72 | nsISupports* context, |
michael@0 | 73 | nsIInputStream *aIStream, |
michael@0 | 74 | uint32_t aSourceOffset, |
michael@0 | 75 | uint32_t aLength) |
michael@0 | 76 | { |
michael@0 | 77 | LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n", |
michael@0 | 78 | aSourceOffset, aLength)); |
michael@0 | 79 | char buf[1025]; |
michael@0 | 80 | uint32_t amt; |
michael@0 | 81 | while (1) { |
michael@0 | 82 | aIStream->Read(buf, 1024, &amt); |
michael@0 | 83 | if (amt == 0) |
michael@0 | 84 | break; |
michael@0 | 85 | buf[amt] = '\0'; |
michael@0 | 86 | puts(buf); |
michael@0 | 87 | } |
michael@0 | 88 | return NS_OK; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | NS_IMETHODIMP |
michael@0 | 92 | TestListener::OnStopRequest(nsIRequest* request, nsISupports* context, |
michael@0 | 93 | nsresult aStatus) |
michael@0 | 94 | { |
michael@0 | 95 | LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus)); |
michael@0 | 96 | gKeepRunning = 0; |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // |
michael@0 | 101 | //---------------------------------------------------------------------------- |
michael@0 | 102 | // Test Provider |
michael@0 | 103 | //---------------------------------------------------------------------------- |
michael@0 | 104 | // |
michael@0 | 105 | |
michael@0 | 106 | class TestProvider : public nsIStreamProvider |
michael@0 | 107 | { |
michael@0 | 108 | public: |
michael@0 | 109 | TestProvider(char *data); |
michael@0 | 110 | virtual ~TestProvider(); |
michael@0 | 111 | |
michael@0 | 112 | NS_DECL_ISUPPORTS |
michael@0 | 113 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 114 | NS_DECL_NSISTREAMPROVIDER |
michael@0 | 115 | |
michael@0 | 116 | protected: |
michael@0 | 117 | nsCOMPtr<nsIByteArrayInputStream> mData; |
michael@0 | 118 | }; |
michael@0 | 119 | |
michael@0 | 120 | NS_IMPL_ISUPPORTS(TestProvider, |
michael@0 | 121 | nsIStreamProvider, |
michael@0 | 122 | nsIRequestObserver) |
michael@0 | 123 | |
michael@0 | 124 | TestProvider::TestProvider(char *data) |
michael@0 | 125 | { |
michael@0 | 126 | NS_NewByteArrayInputStream(getter_AddRefs(mData), data, strlen(data)); |
michael@0 | 127 | LOG(("Constructing TestProvider [this=%p]\n", this)); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | TestProvider::~TestProvider() |
michael@0 | 131 | { |
michael@0 | 132 | LOG(("Destroying TestProvider [this=%p]\n", this)); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | NS_IMETHODIMP |
michael@0 | 136 | TestProvider::OnStartRequest(nsIRequest* request, nsISupports* context) |
michael@0 | 137 | { |
michael@0 | 138 | LOG(("TestProvider::OnStartRequest [this=%p]\n", this)); |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | NS_IMETHODIMP |
michael@0 | 143 | TestProvider::OnStopRequest(nsIRequest* request, nsISupports* context, |
michael@0 | 144 | nsresult aStatus) |
michael@0 | 145 | { |
michael@0 | 146 | LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus)); |
michael@0 | 147 | |
michael@0 | 148 | nsCOMPtr<nsIStreamListener> listener = do_QueryInterface(new TestListener()); |
michael@0 | 149 | |
michael@0 | 150 | if (NS_SUCCEEDED(aStatus)) { |
michael@0 | 151 | nsCOMPtr<nsITransportRequest> treq = do_QueryInterface(request); |
michael@0 | 152 | nsCOMPtr<nsITransport> transport; |
michael@0 | 153 | treq->GetTransport(getter_AddRefs(transport)); |
michael@0 | 154 | if (transport) { |
michael@0 | 155 | nsCOMPtr<nsIRequest> readRequest; |
michael@0 | 156 | transport->AsyncRead(listener, nullptr, 0, 0, 0, getter_AddRefs(readRequest)); |
michael@0 | 157 | } |
michael@0 | 158 | } else |
michael@0 | 159 | gKeepRunning = 0; |
michael@0 | 160 | |
michael@0 | 161 | return NS_OK; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHODIMP |
michael@0 | 165 | TestProvider::OnDataWritable(nsIRequest *request, nsISupports *context, |
michael@0 | 166 | nsIOutputStream *output, uint32_t offset, uint32_t count) |
michael@0 | 167 | { |
michael@0 | 168 | LOG(("TestProvider::OnDataWritable [offset=%u, count=%u]\n", offset, count)); |
michael@0 | 169 | uint32_t writeCount; |
michael@0 | 170 | nsresult rv = output->WriteFrom(mData, count, &writeCount); |
michael@0 | 171 | // Zero bytes written on success indicates EOF |
michael@0 | 172 | if (NS_SUCCEEDED(rv) && (writeCount == 0)) |
michael@0 | 173 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 174 | return rv; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | // |
michael@0 | 178 | //---------------------------------------------------------------------------- |
michael@0 | 179 | // Synchronous IO |
michael@0 | 180 | //---------------------------------------------------------------------------- |
michael@0 | 181 | // |
michael@0 | 182 | nsresult |
michael@0 | 183 | WriteRequest(nsIOutputStream *os, const char *request) |
michael@0 | 184 | { |
michael@0 | 185 | LOG(("WriteRequest [request=%s]\n", request)); |
michael@0 | 186 | uint32_t n; |
michael@0 | 187 | return os->Write(request, strlen(request), &n); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | nsresult |
michael@0 | 191 | ReadResponse(nsIInputStream *is) |
michael@0 | 192 | { |
michael@0 | 193 | uint32_t bytesRead; |
michael@0 | 194 | char buf[2048]; |
michael@0 | 195 | do { |
michael@0 | 196 | is->Read(buf, sizeof(buf), &bytesRead); |
michael@0 | 197 | if (bytesRead > 0) |
michael@0 | 198 | fwrite(buf, 1, bytesRead, stdout); |
michael@0 | 199 | } while (bytesRead > 0); |
michael@0 | 200 | return NS_OK; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | // |
michael@0 | 204 | //---------------------------------------------------------------------------- |
michael@0 | 205 | // Startup... |
michael@0 | 206 | //---------------------------------------------------------------------------- |
michael@0 | 207 | // |
michael@0 | 208 | |
michael@0 | 209 | void |
michael@0 | 210 | sighandler(int sig) |
michael@0 | 211 | { |
michael@0 | 212 | LOG(("got signal: %d\n", sig)); |
michael@0 | 213 | NS_BREAK(); |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | void |
michael@0 | 217 | usage(char **argv) |
michael@0 | 218 | { |
michael@0 | 219 | printf("usage: %s [-sync] <host> <path>\n", argv[0]); |
michael@0 | 220 | exit(1); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | int |
michael@0 | 224 | main(int argc, char* argv[]) |
michael@0 | 225 | { |
michael@0 | 226 | nsresult rv; |
michael@0 | 227 | |
michael@0 | 228 | signal(SIGSEGV, sighandler); |
michael@0 | 229 | |
michael@0 | 230 | #if defined(PR_LOGGING) |
michael@0 | 231 | gTestSocketIOLog = PR_NewLogModule("TestSocketIO"); |
michael@0 | 232 | #endif |
michael@0 | 233 | |
michael@0 | 234 | if (argc < 3) |
michael@0 | 235 | usage(argv); |
michael@0 | 236 | |
michael@0 | 237 | int i=0; |
michael@0 | 238 | bool sync = false; |
michael@0 | 239 | if (nsCRT::strcasecmp(argv[1], "-sync") == 0) { |
michael@0 | 240 | if (argc < 4) |
michael@0 | 241 | usage(argv); |
michael@0 | 242 | sync = true; |
michael@0 | 243 | i = 1; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | char *hostName = argv[1+i]; |
michael@0 | 247 | char *fileName = argv[2+i]; |
michael@0 | 248 | int port = 80; |
michael@0 | 249 | |
michael@0 | 250 | // Create the Event Queue for this thread... |
michael@0 | 251 | nsCOMPtr<nsIEventQueueService> eventQService = |
michael@0 | 252 | do_GetService(kEventQueueServiceCID, &rv); |
michael@0 | 253 | if (NS_FAILED(rv)) { |
michael@0 | 254 | NS_WARNING("failed to create: event queue service!"); |
michael@0 | 255 | return rv; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | rv = eventQService->CreateMonitoredThreadEventQueue(); |
michael@0 | 259 | if (NS_FAILED(rv)) { |
michael@0 | 260 | NS_WARNING("failed to create: thread event queue!"); |
michael@0 | 261 | return rv; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ); |
michael@0 | 265 | |
michael@0 | 266 | // Create the Socket transport service... |
michael@0 | 267 | nsCOMPtr<nsISocketTransportService> sts = |
michael@0 | 268 | do_GetService(kSocketTransportServiceCID, &rv); |
michael@0 | 269 | if (NS_FAILED(rv)) { |
michael@0 | 270 | NS_WARNING("failed to create: socket transport service!"); |
michael@0 | 271 | return rv; |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF |
michael@0 | 275 | "host: %s" CRLF |
michael@0 | 276 | "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF |
michael@0 | 277 | "accept: */*" CRLF |
michael@0 | 278 | "accept-language: en" CRLF |
michael@0 | 279 | "accept-encoding: gzip,deflate,compress,identity" CRLF |
michael@0 | 280 | "keep-alive: 300" CRLF |
michael@0 | 281 | "connection: keep-alive" CRLF |
michael@0 | 282 | CRLF, |
michael@0 | 283 | fileName, hostName); |
michael@0 | 284 | LOG(("Request [\n%s]\n", buffer)); |
michael@0 | 285 | |
michael@0 | 286 | // Create the socket transport... |
michael@0 | 287 | nsCOMPtr<nsITransport> transport; |
michael@0 | 288 | rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, getter_AddRefs(transport)); |
michael@0 | 289 | if (NS_FAILED(rv)) { |
michael@0 | 290 | NS_WARNING("failed to create: socket transport!"); |
michael@0 | 291 | return rv; |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | gElapsedTime = PR_Now(); |
michael@0 | 295 | |
michael@0 | 296 | if (!sync) { |
michael@0 | 297 | nsCOMPtr<nsIRequest> request; |
michael@0 | 298 | rv = transport->AsyncWrite(new TestProvider(buffer), nullptr, 0, 0, 0, getter_AddRefs(request)); |
michael@0 | 299 | if (NS_FAILED(rv)) { |
michael@0 | 300 | NS_WARNING("failed calling: AsyncWrite!"); |
michael@0 | 301 | return rv; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | // Enter the message pump to allow the URL load to proceed. |
michael@0 | 305 | while ( gKeepRunning ) { |
michael@0 | 306 | PLEvent *gEvent; |
michael@0 | 307 | gEventQ->WaitForEvent(&gEvent); |
michael@0 | 308 | gEventQ->HandleEvent(gEvent); |
michael@0 | 309 | } |
michael@0 | 310 | } |
michael@0 | 311 | else { |
michael@0 | 312 | // synchronous write |
michael@0 | 313 | { |
michael@0 | 314 | nsCOMPtr<nsIOutputStream> os; |
michael@0 | 315 | rv = transport->OpenOutputStream(0, 0, 0, getter_AddRefs(os)); |
michael@0 | 316 | if (NS_FAILED(rv)) { |
michael@0 | 317 | LOG(("OpenOutputStream failed [rv=%x]\n", rv)); |
michael@0 | 318 | return rv; |
michael@0 | 319 | } |
michael@0 | 320 | rv = WriteRequest(os, buffer); |
michael@0 | 321 | if (NS_FAILED(rv)) { |
michael@0 | 322 | LOG(("WriteRequest failed [rv=%x]\n", rv)); |
michael@0 | 323 | return rv; |
michael@0 | 324 | } |
michael@0 | 325 | } |
michael@0 | 326 | // synchronous read |
michael@0 | 327 | { |
michael@0 | 328 | nsCOMPtr<nsIInputStream> is; |
michael@0 | 329 | rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(is)); |
michael@0 | 330 | if (NS_FAILED(rv)) { |
michael@0 | 331 | LOG(("OpenInputStream failed [rv=%x]\n", rv)); |
michael@0 | 332 | return rv; |
michael@0 | 333 | } |
michael@0 | 334 | rv = ReadResponse(is); |
michael@0 | 335 | if (NS_FAILED(rv)) { |
michael@0 | 336 | LOG(("ReadResponse failed [rv=%x]\n", rv)); |
michael@0 | 337 | return rv; |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | PRTime endTime; |
michael@0 | 343 | endTime = PR_Now(); |
michael@0 | 344 | LOG(("Elapsed time: %d\n", (int32_t)(endTime/1000UL - gElapsedTime/1000UL))); |
michael@0 | 345 | |
michael@0 | 346 | sts->Shutdown(); |
michael@0 | 347 | return 0; |
michael@0 | 348 | } |
michael@0 | 349 |