<commit_before>/*
 * Copyright 2006 ThoughtWorks, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.thoughtworks.selenium;

import com.google.common.collect.Lists;
import org.openqa.selenium.net.Urls;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.ConnectException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * Sends commands and retrieves results via HTTP.
 * @author <NAME>, <NAME>
 */
public class HttpCommandProcessor implements CommandProcessor {

    private String pathToServlet;
    private String browserStartCommand;
    private String browserURL;
    private String sessionId;
    private String extensionJs;
    private String rcServerLocation;

    /** Specifies a server host/port, a command to launch the browser, and a starting URL for the browser.
     * 
     * @param serverHost - the host name on which the Selenium Server resides
     * @param serverPort - the port on which the Selenium Server is listening
     * @param browserStartCommand - the command string used to launch the browser, e.g. "*firefox" or "c:\\program files\\internet explorer\\iexplore.exe"
     * @param browserURL - the starting URL including just a domain name.  We'll start the browser pointing at the Selenium resources on this URL,
     * @param extensionJs - extension Javascript for this session
     * e.g. "http://www.google.com" would send the browser to "http://www.google.com/selenium-server/core/RemoteRunner.html"
     */
    public HttpCommandProcessor(String serverHost, int serverPort, String browserStartCommand, String browserURL) {
        rcServerLocation = serverHost +
        ":"+ Integer.toString(serverPort);
        this.pathToServlet = "http://" + rcServerLocation + "/selenium-server/driver/";
        this.browserStartCommand = browserStartCommand;
        this.browserURL = browserURL;
        this.extensionJs = "";
    }
    
    /** Specifies the URL to the CommandBridge servlet, a command to launch the browser, and a starting URL for the browser.
     * 
     * @param pathToServlet - the URL of the Selenium Server Driver, e.g. "http://localhost:4444/selenium-server/driver/" (don't forget the final slash!)
     * @param browserStartCommand - the command string used to launch the browser, e.g. "*firefox" or "c:\\program files\\internet explorer\\iexplore.exe"
     * @param browserURL - the starting URL including just a domain name.  We'll start the browser pointing at the Selenium resources on this URL,
     * @param extensionJs - extension Javascript for this session
     */
    public HttpCommandProcessor(String pathToServlet, String browserStartCommand, String browserURL) {
        this.pathToServlet = pathToServlet;
        this.browserStartCommand = browserStartCommand;
        this.browserURL = browserURL;
        this.extensionJs = "";
    }

    public String getRemoteControlServerLocation() {
        return rcServerLocation;
    }

    public String doCommand(String commandName, String[] args) {
        DefaultRemoteCommand command = new DefaultRemoteCommand(commandName,args);
        String result = executeCommandOnServlet(command.getCommandURLString());
        if (result == null) {
            throw new NullPointerException("Selenium Bug! result must not be null");
        }
        if (!result.startsWith("OK")) {
            return throwAssertionFailureExceptionOrError(result);
        }
        return result;
    }

    protected String throwAssertionFailureExceptionOrError(String message) {
        throw new SeleniumException(message);
    }

    /** Sends the specified command string to the bridge servlet */  
    public String executeCommandOnServlet(String command) {
        InputStream is = null;
        try {
            return getCommandResponseAsString(command);
        } catch (IOException e) {
            if (e instanceof ConnectException) {
                throw new SeleniumException(e.getMessage(),e);
            }
            e.printStackTrace();
            throw new UnsupportedOperationException("Catch body broken: IOException from " + command + " -> " + e, e);
        }
    }

    private String stringContentsOfInputStream(Reader rdr) throws IOException {
        StringBuffer sb = new StringBuffer();
        int c;
        try {
          while ((c = rdr.read()) != -1) {
              sb.append((char) c);
          }
          return sb.toString();
        } finally {
          rdr.close();
        }
    }
    
    // for testing
    protected HttpURLConnection getHttpUrlConnection(URL urlForServlet) throws IOException {
      return (HttpURLConnection) urlForServlet.openConnection();
    }
    
    // for testing
    protected Writer getOutputStreamWriter(HttpURLConnection conn) throws IOException {
      return new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); 
    }
    
    // for testing
    protected Reader getInputStreamReader(HttpURLConnection conn) throws IOException {
      return new InputStreamReader(conn.getInputStream(), "UTF-8");
    }
    <commit_msg>SimonStewart: Enforcing utf8 in the http command processer<commit_after>/*
 * Copyright 2006 ThoughtWorks, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.thoughtworks.selenium;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import org.openqa.selenium.net.Urls;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.ConnectException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * Sends commands and retrieves results via HTTP.
 * @author <NAME>, <NAME>
 */
public class HttpCommandProcessor implements CommandProcessor {

    private String pathToServlet;
    private String browserStartCommand;
    private String browserURL;
    private String sessionId;
    private String extensionJs;
    private String rcServerLocation;

    /** Specifies a server host/port, a command to launch the browser, and a starting URL for the browser.
     * 
     * @param serverHost - the host name on which the Selenium Server resides
     * @param serverPort - the port on which the Selenium Server is listening
     * @param browserStartCommand - the command string used to launch the browser, e.g. "*firefox" or "c:\\program files\\internet explorer\\iexplore.exe"
     * @param browserURL - the starting URL including just a domain name.  We'll start the browser pointing at the Selenium resources on this URL,
     * @param extensionJs - extension Javascript for this session
     * e.g. "http://www.google.com" would send the browser to "http://www.google.com/selenium-server/core/RemoteRunner.html"
     */
    public HttpCommandProcessor(String serverHost, int serverPort, String browserStartCommand, String browserURL) {
        rcServerLocation = serverHost +
        ":"+ Integer.toString(serverPort);
        this.pathToServlet = "http://" + rcServerLocation + "/selenium-server/driver/";
        this.browserStartCommand = browserStartCommand;
        this.browserURL = browserURL;
        this.extensionJs = "";
    }
    
    /** Specifies the URL to the CommandBridge servlet, a command to launch the browser, and a starting URL for the browser.
     * 
     * @param pathToServlet - the URL of the Selenium Server Driver, e.g. "http://localhost:4444/selenium-server/driver/" (don't forget the final slash!)
     * @param browserStartCommand - the command string used to launch the browser, e.g. "*firefox" or "c:\\program files\\internet explorer\\iexplore.exe"
     * @param browserURL - the starting URL including just a domain name.  We'll start the browser pointing at the Selenium resources on this URL,
     * @param extensionJs - extension Javascript for this session
     */
    public HttpCommandProcessor(String pathToServlet, String browserStartCommand, String browserURL) {
        this.pathToServlet = pathToServlet;
        this.browserStartCommand = browserStartCommand;
        this.browserURL = browserURL;
        this.extensionJs = "";
    }

    public String getRemoteControlServerLocation() {
        return rcServerLocation;
    }

    public String doCommand(String commandName, String[] args) {
        DefaultRemoteCommand command = new DefaultRemoteCommand(commandName,args);
        String result = executeCommandOnServlet(command.getCommandURLString());
        if (result == null) {
            throw new NullPointerException("Selenium Bug! result must not be null");
        }
        if (!result.startsWith("OK")) {
            return throwAssertionFailureExceptionOrError(result);
        }
        return result;
    }

    protected String throwAssertionFailureExceptionOrError(String message) {
        throw new SeleniumException(message);
    }

    /** Sends the specified command string to the bridge servlet */  
    public String executeCommandOnServlet(String command) {
        InputStream is = null;
        try {
            return getCommandResponseAsString(command);
        } catch (IOException e) {
            if (e instanceof ConnectException) {
                throw new SeleniumException(e.getMessage(),e);
            }
            e.printStackTrace();
            throw new UnsupportedOperationException("Catch body broken: IOException from " + command + " -> " + e, e);
        }
    }

    private String stringContentsOfInputStream(Reader rdr) throws IOException {
        StringBuffer sb = new StringBuffer();
        int c;
        try {
          while ((c = rdr.read()) != -1) {
              sb.append((char) c);
          }
          return sb.toString();
        } finally {
          rdr.close();
        }
    }
    
    // for testing
    protected HttpURLConnection getHttpUrlConnection(URL urlForServlet) throws IOException {
      return (HttpURLConnection) urlForServlet.openConnection();
    }
    
    // for testing
    protected Writer getOutputStreamWriter(HttpURLConnection conn) throws IOException {
      return new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), Charsets.UTF_8));
    }
    
    // for testing
    protected Reader getInputStreamReader(HttpURLConnection conn) throws IOException {
      return new InputStreamReader(conn.getInputStream(), "UTF-8");
    }
    
{'fraction_non_alphanumeric': 0.08512803120230626, 'fraction_numerical': 0.002543666270985247, 'mean_word_length': 3.0940645609163484, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '5107665', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2978, 'n_words': 1008}