<commit_before>var Client = require("ringo/httpclient").Client;
var {Request} = require("ringo/webapp/request");
var {MemoryStream} = require("io");
var {merge} = require("ringo/utils/objects");
var responseForStatus = require("./util").responseForStatus;

var app = function(env) {
    var resp;
    var request = new Request(env);
    var url = request.queryParams.url;
    if (url) {
        var parts = url.split("/");
        if (parts[0] !== (request.scheme + ":") || parts[1] !== "") {
            resp = responseForStatus(400, "The url parameter value must be absolute url with same scheme as request.");
        } else {
            // re-issue request
            var host = parts[2];
            var client = new Client();
            var exchange = client.request({
                url: url,
                method: request.method,
                headers: merge({host: host}, request.headers),
                data: request.contentLength && request.input
            });
            exchange.wait();

            resp = {
                status: exchange.status,
                headers: exchange.headers,
                body: new MemoryStream(exchange.contentBytes)
            };
        }
    } else {
        resp = responseForStatus(400, "Request must contain url parameter.");
    }
    return resp;
};

exports.app = app;<commit_msg>Update proxy to support simple pass.<commit_after>var Client = require("ringo/httpclient").Client;
var Request = require("ringo/webapp/request").Request;
var MemoryStream = require("io").MemoryStream;
var merge = require("ringo/utils/objects").merge;
var responseForStatus = require("./util").responseForStatus;
var defer = require("ringo/promise").defer;

var app = exports.app = function(env) {
    var response;
    var request = new Request(env);
    var url = request.queryParams.url;
    if (url) {
        response = proxyPass(request, url);
    } else {
        response = responseForStatus(400, "Request must contain url parameter.");
    }
    return response;
};

var pass = exports.pass = function(config) {
    if (typeof config == "string") {
        config = {url: config};
    }
    return function(env, match) {
        var request = new Request(env);
        var newUrl = config.url + match + (request.queryString ? "?" + request.queryString : "");
        return proxyPass(request, newUrl, config.preserveHost);
    };
};

function proxyPass(request, url, preserveHost) {
    var parts = url.split("/");
    var response;
    if (parts[0] !== (request.scheme + ":") || parts[1] !== "") {
        print(parts);
        response = responseForStatus(400, "The url parameter value must be absolute url with same scheme as request.");
    } else {
        // re-issue request
        var host = parts[2];
        var client = new Client();
        response = defer();
        var exchange = client.request({
            url: url,
            method: request.method,
            headers: preserveHost ? merge({host: host}, request.headers) : request.headers,
            data: request.contentLength && request.input,
            async: true,
            complete: function() {
                response.resolve({
                    status: exchange.status,
                    headers: exchange.headers,
                    body: new MemoryStream(exchange.contentBytes)
                });
            }
        });
    }
    return response;
}

{'fraction_non_alphanumeric': 0.13071512309495897, 'fraction_numerical': 0.005275498241500586, 'mean_word_length': 2.2016885553470917, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '4279553', 'n_tokens_mistral': 914, 'n_tokens_neox': 887, 'n_words': 245}