<commit_before>package net.wigle.wigleandroid;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;

/**
 * Based on   http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
 * Read more: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html#ixzz0iqTJF7SV
 */
final class HttpFileUploader {
  private static final String ENCODING = "UTF-8";
  
  /** don't allow construction */
  private HttpFileUploader(){
  }

    /** 
     * upload utility method.
     *
     * @param urlString the url to POST the file to
     * @param filename the filename to use for the post
     * @param fileParamName the HTML form field name for the file
     * @param fileInputStream an open file stream to the file to post
     * @param params form data fields (key and value)
     * @param res the app resources (needed for looking up SSL cert)
     * @param handler if non-null gets empty messages with updates on progress
     * @param filesize guess at filesize for UI callbacks
     */
  public static String upload( final String urlString, final String filename, final String fileParamName,
                               final FileInputStream fileInputStream, final Map<String,String> params, 
                               final Resources res, final Handler handler, final long filesize,
                               final Context context ) {
    
    URL connectURL = null;
    try{
      connectURL = new URL( urlString );
    }
    catch( Exception ex ){
      ListActivity.error( "MALFORMATED URL: " + ex, ex );
    }
    
    final String lineEnd = "\r\n";
    final String twoHyphens = "--";
    final String boundary = "*****";
    String retval = null;
    HttpURLConnection conn = null;

    try {
      //------------------ CLIENT REQUEST
    
      ListActivity.info("Creating url connection");
      
      // Open a HTTP connection to the URL
      CharsetEncoder enc = Charset.forName( ENCODING ).newEncoder();
      CharBuffer cbuff = CharBuffer.allocate( 1024 );
      ByteBuffer bbuff = ByteBuffer.allocate( 1024 );

      conn = (HttpURLConnection) connectURL.openConnection();
      
      // Allow Inputs
      conn.setDoInput(true);
      // Allow Outputs
      conn.setDoOutput(true);
      // Don't use a cached copy.
      conn.setUseCaches(false);
      conn.setInstanceFollowRedirects( false );
      if ( conn instanceof javax.net.ssl.HttpsURLConnection ) {
          final SSLConfigurator con = SSLConfigurator.getInstance( res );
          con.configure( (javax.net.ssl.HttpsURLConnection) conn );
          ListActivity.info("using ssl! conn: " + conn);
      }
      
      // Use a post method.
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
      
      // chunk large stuff
      conn.setChunkedStreamingMode( 32*1024 );
      // shouldn't have to do this, but it makes their HttpURLConnectionImpl happy
      conn.setRequestProperty("Transfer-Encoding", "chunked");
    
      // connect
      ListActivity.info( "about to connect" );
      conn.connect();
      ListActivity.info( "connected" );
      
      OutputStream connOutputStream = conn.getOutputStream();
      if ( true ) {
        // reflect out the chunking info
        for ( Method meth : connOutputStream.getClass().getMethods() ) {
          // ListActivity.info("meth: " + meth.getName() );
          try {
            if ( "isCached".equals(meth.getName()) || "isChunked".equals(meth.getName())) {<commit_msg>first pass at ssl-fallback testing <commit_after>package net.wigle.wigleandroid;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;

/**
 * Based on   http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html
 * Read more: http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html#ixzz0iqTJF7SV
 */
final class HttpFileUploader {
  private static final String ENCODING = "UTF-8";
  
  /** don't allow construction */
  private HttpFileUploader(){
  }

    /** 
     * upload utility method.
     *
     * @param urlString the url to POST the file to
     * @param filename the filename to use for the post
     * @param fileParamName the HTML form field name for the file
     * @param fileInputStream an open file stream to the file to post
     * @param params form data fields (key and value)
     * @param res the app resources (needed for looking up SSL cert)
     * @param handler if non-null gets empty messages with updates on progress
     * @param filesize guess at filesize for UI callbacks
     */
  public static String upload( final String urlString, final String filename, final String fileParamName,
                               final FileInputStream fileInputStream, final Map<String,String> params, 
                               final Resources res, final Handler handler, final long filesize,
                               final Context context ) {
    
    URL connectURL = null;
    try{
      connectURL = new URL( urlString );
    }
    catch( Exception ex ) {
      ListActivity.error( "MALFORMATED URL: " + ex, ex );
      return null;
    }

    // test if we should be doing our own ssl
    boolean self_serving = true;
    try {
        URLConnection testcon = connectURL.openConnection();
        // we should probably time-bound this test-connection?
        testcon.connect();
        self_serving = false;
        // consider a pref to skip this? or just phase out after migration?
    } catch (IOException ex) {
        // we're specificly interested in javax.net.ssl.SSLException
    }

    
    final String lineEnd = "\r\n";
    final String twoHyphens = "--";
    final String boundary = "*****";
    String retval = null;
    HttpURLConnection conn = null;

    try {
      //------------------ CLIENT REQUEST
    
      ListActivity.info("Creating url connection");
      
      // Open a HTTP connection to the URL
      CharsetEncoder enc = Charset.forName( ENCODING ).newEncoder();
      CharBuffer cbuff = CharBuffer.allocate( 1024 );
      ByteBuffer bbuff = ByteBuffer.allocate( 1024 );

      conn = (HttpURLConnection) connectURL.openConnection();
      
      // Allow Inputs
      conn.setDoInput(true);
      // Allow Outputs
      conn.setDoOutput(true);
      // Don't use a cached copy.
      conn.setUseCaches(false);
      conn.setInstanceFollowRedirects( false );
      if ( ( conn instanceof javax.net.ssl.HttpsURLConnection ) && self_serving ) {
          final SSLConfigurator con = SSLConfigurator.getInstance( res );
          con.configure( (javax.net.ssl.HttpsURLConnection) conn );
          ListActivity.info("using ssl! conn: " + conn);
      }
      
      // Use a post method.
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
      
      // chunk large stuff
      conn.setChunkedStreamingMode( 32*1024 );
      // shouldn't have to do this, but it makes their HttpURLConnectionImpl happy
      conn.setRequestProperty("Transfer-Encoding", "chunked");
    
      // connect
      ListActivity.info( "about to connect" );
      conn.connect();
      ListActivity.info( "connected" );
      
      OutputStream connOutputStream = conn.getOutputStream();
      if ( true ) {
        // reflect out the chunking info
        for ( Method meth : connOutputStream.getClass().getMethods() ) {
          // ListActivity.info("meth: " + meth.getName() );
          try {
            if ( "isCached".equals(meth.getName()) || "isChunked".equals(meth.getName())) {
{'fraction_non_alphanumeric': 0.10696322789761932, 'fraction_numerical': 0.006482619872582989, 'mean_word_length': 2.6990491938817693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783164', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2454, 'n_words': 693}