xpcom/tests/CvtURL.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include <stdio.h>
     6 #include "nscore.h"
     7 #include "nsIConverterInputStream.h"
     8 #include "nsIURL.h"
     9 #include "nsNetUtil.h"
    10 #include "nsCRT.h"
    11 #include "nsString.h"
    12 #include "prprf.h"
    13 #include "prtime.h"
    15 static nsString* ConvertCharacterSetName(const char* aName)
    16 {
    17   return new nsString(NS_ConvertASCIItoUTF16(aName));
    18 }
    20 int main(int argc, char** argv)
    21 {
    22   if (3 != argc) {
    23     printf("usage: CvtURL url utf8\n");
    24     return -1;
    25   }
    27   char* characterSetName = argv[2];
    28   nsString* cset = ConvertCharacterSetName(characterSetName);
    29   if (NS_PTR_TO_INT32(cset) < 0) {
    30     printf("illegal character set name: '%s'\n", characterSetName);
    31     return -1;
    32   }
    34   // Create url object
    35   char* urlName = argv[1];
    36   nsIURI* url;
    37   nsresult rv;
    38   rv = NS_NewURI(&url, urlName);
    39   if (NS_OK != rv) {
    40     printf("invalid URL: '%s'\n", urlName);
    41     return -1;
    42   }
    44   // Get an input stream from the url
    45   nsresult ec;
    46   nsIInputStream* in;
    47   ec = NS_OpenURI(&in, url);
    48   if (nullptr == in) {
    49     printf("open of url('%s') failed: error=%x\n", urlName, ec);
    50     return -1;
    51   }
    53   // Translate the input using the argument character set id into
    54   // unicode
    55   nsCOMPtr<nsIConverterInputStream> uin =
    56     do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv);
    57   if (NS_SUCCEEDED(rv))
    58     rv = uin->Init(in, cset->get(), 4096);
    59   if (NS_FAILED(rv)) {
    60     printf("can't create converter input stream: %d\n", rv);
    61     return -1;
    62   }
    64   // Read the input and write some output
    65   PRTime start = PR_Now();
    66   int32_t count = 0;
    67   for (;;) {
    68     char16_t buf[1000];
    69     uint32_t nb;
    70     ec = uin->Read(buf, 0, 1000, &nb);
    71     if (NS_FAILED(ec)) {
    72       printf("i/o error: %d\n", ec);
    73       break;
    74     }
    75     if (nb == 0) break; // EOF
    76     count += nb;
    77   }
    78   PRTime end = PR_Now();
    79   PRTime conversion = (end - start) / 1000;
    80   char buf[500];
    81   PR_snprintf(buf, sizeof(buf),
    82               "converting and discarding %d bytes took %lldms",
    83               count, conversion);
    84   puts(buf);
    86   // Release the objects
    87   in->Release();
    88   url->Release();
    90   return 0;
    91 }

mercurial