~aquaratixc/ardilla

444177434c543994390d8f074935c3f617c95463 — Oleg Bakharev 1 year, 9 months ago d407a4d main
Modified app.d, patched for non-standard clients and adding improvements to debug mode
3 files changed, 38 insertions(+), 17 deletions(-)

M app.d
M ardilla-debug.do
M colorized.d
M app.d => app.d +25 -11
@@ 6,7 6,7 @@ private {
	import std.string : indexOf, startsWith, strip;
	
	import ardilla.configuration;
	import ardilla.gopher : GOPHER_REQUEST_TERMINATOR;
	import ardilla.gopher : GOPHER_DELIMETER, GOPHER_REQUEST_TERMINATOR;
	import ardilla.responses;
	import ardilla.server;
	


@@ 14,13 14,18 @@ private {
		import std.string : replace;
		import ardilla.colorized;
		
		auto textify(ubyte[] request)
		auto textify(string request)
		{
			return (cast(string) request)
			return request
							.replace("\n", "<LF>")
							.replace("\t", "<TAB>")
							.replace("\r", "<CR>");
		}

		auto textify(ubyte[] request)
		{
			return textify(cast(string) request);
		}
	}
}



@@ 45,7 50,7 @@ class GopherServer : GenericSimpleServer!(GOPHER_BUFFER_SIZE, GOPHER_NUMBER_OF_C
		
		if (request.length >= 1)
		{
			if (request == GOPHER_REQUEST_TERMINATOR) 
			if ((request == GOPHER_REQUEST_TERMINATOR) || (request == [GOPHER_DELIMETER.LF])) 
			{
				debug {
						log("[Client] Menu request: " ~ textify(request));


@@ 64,17 69,22 @@ class GopherServer : GenericSimpleServer!(GOPHER_BUFFER_SIZE, GOPHER_NUMBER_OF_C
					
					debug {
						log("[Client] Query length: " ~ entry[tab+1..$].length.to!string);
						log("[Client] Search for: " ~ entry);
						log("[Client] Search for: " ~ textify(entry));
					}
				}
				else
				{
					if (entry.exists && entry.startsWith(GOPHER_FOLDER))
				    if (!entry.startsWith(GOPHER_FOLDER))
				    {
				    	entry = GOPHER_FOLDER ~ "/" ~ entry; 
				    }
				    
					if (entry.exists)
					{
						if (entry.isDir)
						{
							debug {
								log("[Client] Directory request: " ~ entry);
								log("[Client] Directory request: " ~ textify(entry));
							}
							
							response = GopherResponse.fromPath(entry, GOPHER_MAP_FILENAME, _host, _port.to!string);


@@ 82,7 92,7 @@ class GopherServer : GenericSimpleServer!(GOPHER_BUFFER_SIZE, GOPHER_NUMBER_OF_C
						else
						{
							debug {
								log("[Client] File request: " ~ entry);
								log("[Client] File request: " ~ textify(entry));
							}
							
							response = GopherResponse.fromFile(entry);


@@ 92,7 102,7 @@ class GopherServer : GenericSimpleServer!(GOPHER_BUFFER_SIZE, GOPHER_NUMBER_OF_C
					{
						debug {
							log("[Client] Request: " ~ textify(request));
							error("[Server]: Wrong request: " ~ entry);
							error("[Server]: Wrong request: " ~ textify(entry));
						}
						
						response = GopherResponse.createError("Wrong file or directory");


@@ 109,7 119,11 @@ class GopherServer : GenericSimpleServer!(GOPHER_BUFFER_SIZE, GOPHER_NUMBER_OF_C
void main()
{
	auto gopher = new GopherServer(GOPHER_DOMAIN, GOPHER_FOLDER);
    	

	debug {
		warning(`[Startup]: Server running in debug mode. Please don't use this !!!`);
	}
	    	
	with (gopher)
	{
		debug {


@@ 129,4 143,4 @@ void main()
		
		run;
	}
}
}
\ No newline at end of file

M ardilla-debug.do => ardilla-debug.do +1 -1
@@ 1,4 1,4 @@
#!/bin/sh
DEPS=$(ls *.d | xargs)
redo-ifchange ${DEPS#*:}
ldc2 ${DEPS#*:} -of $3
ldc2 ${DEPS#*:} -d-debug -of $3

M colorized.d => colorized.d +12 -5
@@ 1,26 1,33 @@
module ardilla.colorized;

private {
	import std.datetime : Clock, SysTime;
	import std.stdio : writeln;
	import std.string : format;
	import std.string : format, replace;
}

auto wrapTime(string query)
{
	SysTime currentTime = Clock.currTime;
	return currentTime.toISOExtString.replace("T", " ") ~ " " ~ query;
}

void error(string message) 
{
	format("\u001b[31m\u001b[49m\u001b[1mError:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m", message).writeln;
	format(wrapTime("\u001b[31m\u001b[49m\u001b[1mError:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m"), message).writeln;
}

void info(string message) 
{
	format("\u001b[32m\u001b[49m\u001b[1mInfo:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m", message).writeln;
	format(wrapTime("\u001b[32m\u001b[49m\u001b[1mInfo:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m"), message).writeln;
}

void log(string message) 
{
	format("\u001b[34m\u001b[49m\u001b[1mLog:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m", message).writeln;
	format(wrapTime("\u001b[34m\u001b[49m\u001b[1mLog:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m"), message).writeln;
}

void warning(string message) 
{
	format("\u001b[33m\u001b[49m\u001b[1mWarning:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m", message).writeln;
	format(wrapTime("\u001b[33m\u001b[49m\u001b[1mWarning:\u001b[0m\u001b[97m\u001b[49m\u001b[1m %s \u001b[0m"), message).writeln;
}