<commit_before>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * 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 io.gatling.recorder.http.handler.server

import java.net.InetSocketAddress

import com.ning.http.client.uri.Uri
import io.gatling.recorder.http.HttpProxy
import io.gatling.recorder.http.handler.ScalaChannelHandler
import org.jboss.netty.channel.{ Channel, ChannelFuture }
import org.jboss.netty.handler.codec.http.{ DefaultHttpResponse, HttpRequest, HttpResponseStatus, HttpVersion }

class HttpServerHandler(proxy: HttpProxy) extends ServerHandler(proxy) with ScalaChannelHandler {

  private def buildRequestWithRelativeURI(request: HttpRequest): HttpRequest = {
    val relative = Uri.create(request.getUri).toRelativeUrl
    copyRequestWithNewUri(request, relative)
  }

  private def writeRequest(clientChannel: Channel, request: HttpRequest): Unit = {
    val clientRequest = proxy.outgoingProxy match {
      case None => buildRequestWithRelativeURI(request)
      case _    => request
    }

    writeRequestToClient(clientChannel, clientRequest, request)
  }

  private def writeRequestWithNewChannel(serverChannel: Channel, request: HttpRequest): Unit = {
    _clientChannel = None

    val inetSocketAddress = proxy.outgoingProxy match {
      case Some((host, port)) => new InetSocketAddress(host, port)
      case _ =>
        try {
          computeInetSocketAddress(Uri.create(request.getUri))
        } catch {
          case e: Exception =>
            throw new RuntimeException(s"Could not build address requestURI='${request.getUri}'", e)
        }
    }

    proxy.clientBootstrap
      .connect(inetSocketAddress)
      .addListener { future: ChannelFuture =>
        if (future.isSuccess) {<commit_msg>Improve exception when people directly hit the proxy, close #2204<commit_after>/**
 * Copyright 2011-2014 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
 *
 * 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 io.gatling.recorder.http.handler.server

import java.net.InetSocketAddress

import com.ning.http.client.uri.Uri
import io.gatling.recorder.http.HttpProxy
import io.gatling.recorder.http.handler.ScalaChannelHandler
import org.jboss.netty.channel.{ Channel, ChannelFuture }
import org.jboss.netty.handler.codec.http.{ DefaultHttpResponse, HttpRequest, HttpResponseStatus, HttpVersion }

class HttpServerHandler(proxy: HttpProxy) extends ServerHandler(proxy) with ScalaChannelHandler {

  private def buildRequestWithRelativeURI(request: HttpRequest): HttpRequest = {
    val relative = Uri.create(request.getUri).toRelativeUrl
    copyRequestWithNewUri(request, relative)
  }

  private def writeRequest(clientChannel: Channel, request: HttpRequest): Unit = {
    val clientRequest = proxy.outgoingProxy match {
      case None => buildRequestWithRelativeURI(request)
      case _    => request
    }

    writeRequestToClient(clientChannel, clientRequest, request)
  }

  private def writeRequestWithNewChannel(serverChannel: Channel, request: HttpRequest): Unit = {
    _clientChannel = None

    val inetSocketAddress = proxy.outgoingProxy match {
      case Some((host, port)) =>
        new InetSocketAddress(host, port)

      case None if request.getUri.startsWith("/") =>
        throw new IllegalArgumentException(s"Request url ${request.getUri} is relative, you're probably directly hitting the proxy")

      case None =>
        try {
          computeInetSocketAddress(Uri.create(request.getUri))
        } catch {
          case e: Exception =>
            throw new RuntimeException(s"Could not build address requestURI='${request.getUri}'", e)
        }
    }

    proxy.clientBootstrap
      .connect(inetSocketAddress)
      .addListener { future: ChannelFuture =>
        if (future.isSuccess) {
{'fraction_non_alphanumeric': 0.08585546633724521, 'fraction_numerical': 0.005764875437512868, 'mean_word_length': 3.9470468431771892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '7665373', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1281, 'n_words': 393}