add -p flag

This commit is contained in:
Neale Pickett 2012-02-16 20:50:42 -07:00
parent 9c4a0ce8cc
commit db4455f6c9
3 changed files with 31 additions and 14 deletions

17
eris.c
View File

@ -1456,13 +1456,14 @@ main(int argc, char *argv[], const char *const *envp)
int docgi = 0; int docgi = 0;
int dirlist = 0; int dirlist = 0;
int redirect = 0; int redirect = 0;
int portappend = 0;
int len; int len;
int in; int in;
{ {
int opt; int opt;
while (-1 != (opt = getopt(argc, argv, "cdr"))) { while (-1 != (opt = getopt(argc, argv, "cdrp"))) {
switch (opt) { switch (opt) {
case 'c': case 'c':
docgi = 1; docgi = 1;
@ -1473,8 +1474,11 @@ main(int argc, char *argv[], const char *const *envp)
case 'r': case 'r':
redirect = 1; redirect = 1;
break; break;
case 'p':
portappend = 1;
break;
default: default:
fprintf(stderr, "Usage: %s [-c] [-d] [-r]\n", argv[0]); fprintf(stderr, "Usage: %s [-c] [-d] [-r] [-p]\n", argv[0]);
return 69; return 69;
} }
} }
@ -1651,13 +1655,20 @@ main(int argc, char *argv[], const char *const *envp)
ip = "127.0.0.1"; ip = "127.0.0.1";
if (strlen(ip) + strlen(port) > 90) if (strlen(ip) + strlen(port) > 90)
exit(101); exit(101);
if (portappend) {
sprintf(Buf, "%s:%s", ip, port); sprintf(Buf, "%s:%s", ip, port);
} else {
strcpy(Buf, ip);
}
host = Buf; host = Buf;
} else { } else {
char *colon = strchr(host, ':'); char *colon = strchr(host, ':');
if (!colon) {
if (portappend && !colon) {
sprintf(Buf, "%s:%s", host, port); sprintf(Buf, "%s:%s", host, port);
host = Buf; host = Buf;
} else if (!portappend && colon) {
*colon = '\0';
} }
} }
for (i = strlen(host); i >= 0; --i) for (i = strlen(host); i >= 0; --i)

View File

@ -16,7 +16,7 @@ class ArgTests(unittest.TestCase):
p = eris(*args) p = eris(*args)
so, se = p.communicate(b'GET / HTTP/1.0\r\n\r\n') so, se = p.communicate(b'GET / HTTP/1.0\r\n\r\n')
self.assertRegexpMatches(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 6\r\nLast-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT\r\n\r\njames\n') self.assertRegexpMatches(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 6\r\nLast-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT\r\n\r\njames\n')
self.assertEqual(se, b'10.1.2.3 200 6 127.0.0.1:80 (null) (null) /index.html\n') self.assertEqual(se, b'10.1.2.3 200 6 127.0.0.1 (null) (null) /index.html\n')
def testArgs(self): def testArgs(self):
"Make sure index.html is the same for all arguments" "Make sure index.html is the same for all arguments"
@ -26,6 +26,12 @@ class ArgTests(unittest.TestCase):
self.check_index('-r') self.check_index('-r')
self.check_index('-c') self.check_index('-c')
def testPortAppend(self):
p = eris('-p')
so, se = p.communicate(b'GET / HTTP/1.0\r\n\r\n')
self.assertRegexpMatches(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 6\r\nLast-Modified: (Mon|Tue|Wed|Thu|Fri|Sat|Sun), .. (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) 2... ..:..:.. GMT\r\n\r\njames\n')
self.assertEqual(se, b'10.1.2.3 200 6 127.0.0.1:80 (null) (null) /index.html\n')
def testBadArgs(self): def testBadArgs(self):
"Make sure bad arguments actually fail" "Make sure bad arguments actually fail"
@ -57,18 +63,18 @@ class DirTests(BasicTests):
def testRootDir(self): def testRootDir(self):
so, se = self.get('/', 'empty') so, se = self.get('/', 'empty')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h3>Directory Listing: /</h3>\n<pre>\n</pre>\n') self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h3>Directory Listing: /</h3>\n<pre>\n</pre>\n')
self.assertEqual(se, b'10.1.2.3 200 32 empty:80 (null) (null) /\n') self.assertEqual(se, b'10.1.2.3 200 32 empty (null) (null) /\n')
def testNoTrailingSlash(self): def testNoTrailingSlash(self):
so, se = self.get('/files', 'default') so, se = self.get('/files', 'default')
self.assertEqual(so, b'HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Length: 50\r\nContent-Type: text/html\r\n\r\n<title>Not Found</title>No such file or directory.') self.assertEqual(so, b'HTTP/1.0 404 Not Found\r\nConnection: close\r\nContent-Length: 50\r\nContent-Type: text/html\r\n\r\n<title>Not Found</title>No such file or directory.')
self.assertEqual(se, b'10.1.2.3 404 0 default:80 (null) (null) /files\n') self.assertEqual(se, b'10.1.2.3 404 0 default (null) (null) /files\n')
def testFiles(self): def testFiles(self):
so, se = self.get('/files/', 'default') so, se = self.get('/files/', 'default')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h3>Directory Listing: /files/</h3>\n<pre>\n<a href="/">Parent directory</a>\n[TXT] <a href="1.txt">1.txt</a>\n</pre>\n') self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<h3>Directory Listing: /files/</h3>\n<pre>\n<a href="/">Parent directory</a>\n[TXT] <a href="1.txt">1.txt</a>\n</pre>\n')
self.assertEqual(se, b'10.1.2.3 200 110 default:80 (null) (null) /files/\n') self.assertEqual(se, b'10.1.2.3 200 110 default (null) (null) /files/\n')
class CGITests(BasicTests): class CGITests(BasicTests):
@ -76,18 +82,18 @@ class CGITests(BasicTests):
def testSet(self): def testSet(self):
so, se = self.get('/cgi/set.cgi', 'default') so, se = self.get('/cgi/set.cgi', 'default')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default:80\nSERVER_PORT:80\nREQUEST_METHOD:GET\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\n') self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default\nSERVER_PORT:80\nREQUEST_METHOD:GET\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\n')
self.assertEqual(se, b'10.1.2.3 200 245 default:80 (null) (null) /cgi/set.cgi\n') self.assertEqual(se, b'10.1.2.3 200 242 default (null) (null) /cgi/set.cgi\n')
def testSetArgs(self): def testSetArgs(self):
so, se = self.get('/cgi/set.cgi?a=1&b=2&c=3', 'default') so, se = self.get('/cgi/set.cgi?a=1&b=2&c=3', 'default')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default:80\nSERVER_PORT:80\nREQUEST_METHOD:GET\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\nQUERY_STRING:a=1&b=2&c=3\n') self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default\nSERVER_PORT:80\nREQUEST_METHOD:GET\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\nQUERY_STRING:a=1&b=2&c=3\n')
self.assertEqual(se, b'10.1.2.3 200 270 default:80 (null) (null) /cgi/set.cgi\n') self.assertEqual(se, b'10.1.2.3 200 267 default (null) (null) /cgi/set.cgi\n')
def testPost(self): def testPost(self):
so, se = self.post('/cgi/set.cgi', 'default', 'a=1&b=2&c=3') so, se = self.post('/cgi/set.cgi', 'default', 'a=1&b=2&c=3')
self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default:80\nSERVER_PORT:80\nREQUEST_METHOD:POST\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\nCONTENT_TYPE:application/x-www-form-urlencoded\nCONTENT_LENGTH:11\nForm data: a=1&b=2&c=3') self.assertEqual(so, b'HTTP/1.0 200 OK\r\nServer: eris/2\r\nPragma: no-cache\r\nConnection: close\r\nContent-Type: text/plain\r\n\r\nGATEWAY_INTERFACE:CGI/1.1\nSERVER_PROTOCOL:HTTP/1.0\nSERVER_SOFTWARE:eris/2\nSERVER_NAME:default\nSERVER_PORT:80\nREQUEST_METHOD:POST\nREQUEST_URI:/cgi/set.cgi\nSCRIPT_NAME:/cgi/set.cgi\nREMOTE_ADDR:10.1.2.3\nREMOTE_PORT:5858\nCONTENT_TYPE:application/x-www-form-urlencoded\nCONTENT_LENGTH:11\nForm data: a=1&b=2&c=3')
self.assertEqual(se, b'10.1.2.3 200 333 default:80 (null) (null) /cgi/set.cgi\n') self.assertEqual(se, b'10.1.2.3 200 330 default (null) (null) /cgi/set.cgi\n')
unittest.main() unittest.main()