<commit_before>// This file is part of cget, copyright (c) 2015 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

import * as fs from 'fs';
import * as url from 'url';
import * as http from 'http';

import {fsa, extend} from '../dist/cget/util';
import {FetchOptions, Cache, CacheResult} from '../dist/cget/Cache';

var cache = new Cache(process.argv[2], process.argv[3]);

type ArgTbl = {[key: string]: string};

function parseArgs(query: string) {
	var result: ArgTbl = {};

	if(query) {
		for(var item of query.split('&')) {
			var partList = item.split('=').map(decodeURIComponent);

			if(partList.length == 2) result[partList[0]] = partList[1];
		}
	}

	return(result);
}

function reportError(res: http.ServerResponse, code: number, header?: Object) {
	var body = new Buffer(code + '\n', 'utf-8');

	header = extend(
		header || {},
		{
			'Content-Type': 'text/plain',
			'Content-Length': body.length
		}
	)

	res.writeHead(code, header);

	res.end(body);
}

var app = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
	var urlParts = url.parse(req.url);
	var args = parseArgs(urlParts.query);
	var host = args['host'];

	if(!host) {
		reportError(res, 400);
		return;
	}

	urlParts.protocol = 'http';
	urlParts.search = null;
	urlParts.query = null;
	urlParts.host = host;

	var cachePath = cache.getCachePath(url.format(urlParts));

	cachePath.then(Cache.checkRemoteLink).then((urlRemote: string) => {
		if(urlRemote) {
			reportError(res, 302, {
				'Location': urlRemote
			});
<commit_msg>Add URN support to test server.<commit_after>// This file is part of cget, copyright (c) 2015-2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.

import * as fs from 'fs';
import * as url from 'url';
import * as http from 'http';

import {fsa, extend} from '../dist/cget/util';
import {Address, Cache, CacheResult} from '../dist/cget';

var cache = new Cache(process.argv[2], process.argv[3]);

type ArgTbl = {[key: string]: string};

function parseArgs(query: string) {
	var result: ArgTbl = {};

	if(query) {
		for(var item of query.split('&')) {
			var partList = item.split('=').map(decodeURIComponent);

			if(partList.length == 2) result[partList[0]] = partList[1];
		}
	}

	return(result);
}

function reportError(res: http.ServerResponse, code: number, header?: Object) {
	var body = new Buffer(code + '\n', 'utf-8');

	header = extend(
		header || {},
		{
			'Content-Type': 'text/plain',
			'Content-Length': body.length
		}
	)

	res.writeHead(code, header);

	res.end(body);
}

var app = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
	var urlParts = url.parse(req.url);
	var args = parseArgs(urlParts.query);
	var host = args['host'];

	if(!host) {
		reportError(res, 400);
		return;
	}

	urlParts.protocol = 'http';
	urlParts.search = null;
	urlParts.query = null;
	urlParts.host = host;

	var cachePath = cache.getCachePath(new Address(url.format(urlParts)));

	cachePath.then(Cache.checkRemoteLink).then((urlRemote: string) => {
		if(urlRemote) {
			reportError(res, 302, {
				'Location': urlRemote
			});

{'fraction_non_alphanumeric': 0.1680051397365885, 'fraction_numerical': 0.011564407324124639, 'mean_word_length': 4.121710526315789, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1492019', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1080, 'n_words': 274}