Ticket #14859: 0001-Do-not-proceed-with-lookup-if-an-encoding-error-is-r.patch

File 0001-Do-not-proceed-with-lookup-if-an-encoding-error-is-r.patch, 4.7 KB (added by iwakeh, 5 years ago)

patch suggestion

  • src/main/java/org/torproject/onionoo/updater/LookupService.java

    From 8265e6ac4aef8211990e7e1986547f97e8c02dde Sun Feb 22 21:00:00 2015
    From: iwakeh <iwakeh@users.ourproject.org>
    Date: Sun, 22 Jan 2015 21:00:00 +0000
    Subject: [PATCH] Do not proceed with lookup if an encoding error is reported
     and log file encoding errors.
    
    ---
     .../torproject/onionoo/updater/LookupService.java  | 38 +++++++++++++++-------
     1 file changed, 26 insertions(+), 12 deletions(-)
    
    diff --git a/src/main/java/org/torproject/onionoo/updater/LookupService.java b/src/main/java/org/torproject/onionoo/updater/LookupService.java
    index f88e752..0b8aa59 100644
    a b package org.torproject.onionoo.updater; 
    55import java.io.BufferedReader;
    66import java.io.File;
    77import java.io.FileInputStream;
     8import java.io.FileNotFoundException;
    89import java.io.IOException;
    910import java.io.InputStreamReader;
     11import java.nio.charset.CharacterCodingException;
     12import java.nio.charset.CharsetDecoder;
     13import java.nio.charset.CodingErrorAction;
     14import java.nio.charset.StandardCharsets;
    1015import java.util.HashMap;
    1116import java.util.HashSet;
    1217import java.util.Map;
    import java.util.SortedSet; 
    1621import java.util.TreeMap;
    1722import java.util.TreeSet;
    1823import java.util.regex.Pattern;
    19 
    2024import org.slf4j.Logger;
    2125import org.slf4j.LoggerFactory;
    2226import org.torproject.onionoo.util.FormattingUtils;
    public class LookupService { 
    114118    try {
    115119      SortedSet<Long> sortedAddressNumbers = new TreeSet<Long>(
    116120          addressStringNumbers.values());
    117       BufferedReader br = new BufferedReader(new InputStreamReader(
    118           new FileInputStream(this.geoLite2CityBlocksIPv4CsvFile),
    119           "UTF-8"));
     121      BufferedReader br = createBufferedReaderFromUtf8File(
     122          this.geoLite2CityBlocksIPv4CsvFile);
    120123      String line = br.readLine();
    121124      while ((line = br.readLine()) != null) {
    122125        String[] parts = line.split(",", 9);
    public class LookupService { 
    177180      br.close();
    178181    } catch (IOException e) {
    179182      log.error("I/O exception while reading "
    180           + this.geoLite2CityBlocksIPv4CsvFile.getAbsolutePath() + ".");
     183          + this.geoLite2CityBlocksIPv4CsvFile.getAbsolutePath()
     184          + ": " + e);
    181185      return lookupResults;
    182186    }
    183187
    public class LookupService { 
    186190    try {
    187191      Set<Long> blockNumbers = new HashSet<Long>(
    188192          addressNumberBlocks.values());
    189       BufferedReader br = new BufferedReader(new InputStreamReader(
    190           new FileInputStream(this.geoLite2CityLocationsEnCsvFile),
    191           "UTF-8"));
     193      BufferedReader br = createBufferedReaderFromUtf8File(
     194          this.geoLite2CityLocationsEnCsvFile);
    192195      String line = br.readLine();
    193196      while ((line = br.readLine()) != null) {
    194197        String[] parts = line.replaceAll("\"", "").split(",", 13);
    public class LookupService { 
    217220      br.close();
    218221    } catch (IOException e) {
    219222      log.error("I/O exception while reading "
    220           + this.geoLite2CityLocationsEnCsvFile.getAbsolutePath() + ".");
     223          + this.geoLite2CityLocationsEnCsvFile.getAbsolutePath()
     224          + ": " + e);
    221225      return lookupResults;
    222226    }
    223227
    public class LookupService { 
    227231      SortedSet<Long> sortedAddressNumbers = new TreeSet<Long>(
    228232          addressStringNumbers.values());
    229233      long firstAddressNumber = sortedAddressNumbers.first();
    230       BufferedReader br = new BufferedReader(new InputStreamReader(
    231           new FileInputStream(geoIPASNum2CsvFile), "UTF-8"));
     234      BufferedReader br = createBufferedReaderFromUtf8File(
     235          this.geoIPASNum2CsvFile);
    232236      String line;
    233237      long previousStartIpNum = -1L;
    234238      while ((line = br.readLine()) != null) {
    public class LookupService { 
    285289      br.close();
    286290    } catch (IOException e) {
    287291      log.error("I/O exception while reading "
    288           + geoIPASNum2CsvFile.getAbsolutePath() + ".");
     292          + geoIPASNum2CsvFile.getAbsolutePath() + ": " + e);
    289293      return lookupResults;
    290294    }
    291295
    public class LookupService { 
    349353        addressesResolved) + " addresses resolved\n");
    350354    return sb.toString();
    351355  }
     356
     357  private BufferedReader createBufferedReaderFromUtf8File(File utf8File)
     358      throws FileNotFoundException, CharacterCodingException {
     359    CharsetDecoder dec = StandardCharsets.UTF_8.newDecoder();
     360    dec.onMalformedInput(CodingErrorAction.REPORT);
     361    dec.onUnmappableCharacter(CodingErrorAction.REPORT);
     362          BufferedReader br = new BufferedReader(new InputStreamReader(
     363        new FileInputStream(utf8File), dec));
     364    return br;
     365  }
    352366}