forked from wunderlins/webpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.py
executable file
·171 lines (142 loc) · 4.44 KB
/
httpd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Start application server on port 8080
The first command line argument will set the port to be bound. Remeber, you
need root privvileges to bind ports below 1024.
"""
# setup paths
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'etc'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib', 'web'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib', 'wsgilog'))
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'lib', 'python-mimeparse-1.5.1'))
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'lib', 'mimerender-master', 'src'))
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'lib', 'dicttoxml-1.6.6'))
# import modules
import web, config, json
import datetime
import time
import tempfile
import sys, logging
from wsgilog import WsgiLog
from cgi import escape
import usbauth
import hashlib
import sqlite3
import webctx
meta = {
"name": config.meta_name,
"version": config.meta_version
}
## global variables ############################################################
# url to class mapping
urls = (
'/', 'webctx.index',
'/env', 'webctx.env',
'/json1', 'webctx.json1',
'/json2', 'webctx.json2',
'/image', 'webctx.image',
'/login', 'webctx.login',
'/bootstrap', 'webctx.bootstrap',
'/authorisation', 'webctx.authorisationxmpl',
'/resttest', 'webctx.resttest',
'/rest(.*)', 'webctx.rest'
)
# default session values
session_default = {
"uid": -1,
"user": None,
"email": None
}
## webpy extensions ############################################################
class service(web.application):
""" allow to pass a custom port/ip into the application """
def run(self, port=8080, ip='0.0.0.0', *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, (ip, port))
class Log(WsgiLog):
""" extend logger, logging to file in var/ """
def __init__(self, application):
WsgiLog.__init__(
self,
application,
logformat = '%(message)s',
tofile = True,
toprint = True,
file = config.app_logfile,
#when = "D",
#interval = 1,
#backups = "1000"
)
class hooks(object):
@staticmethod
def load():
web.debug("Loadhook")
#web.debug(webctx.session.uid)
#return "BEGIN"
@staticmethod
def unload():
web.debug("Unloadhook")
#return "ENDE"
# redirect webserver logs to file
#weblog = open(config.web_logfile, "ab")
#sys.stderr = weblog
#sys.stdout = weblog
def init_session(app):
#global app
web.debug(web.config.get('_session'))
if web.config.get('_session') is None:
web.debug("Setting up new session ...")
web.config.session_parameters['cookie_name'] = meta["name"]
web.config.session_parameters['timeout'] = config.session_timeout,
web.config.session_parameters['secret_key'] = config.session_salt
web.config.session_parameters['cookie_domain'] = config.session_cookie_domain
web.config.session_parameters['ignore_expiry'] = config.session_ignore_expiry
web.config.session_parameters['ignore_change_ip'] = config.session_ignore_change_ip
web.config.session_parameters['expired_message'] = config.session_expired_message
temp = tempfile.mkdtemp(dir=config.session_dir, prefix=config.session_dir_prefix)
webctx.session = web.session.Session(
app,
web.session.DiskStore(temp),
initializer = session_default
)
#for k in session_default.keys():
# webctx.session[k] = session_default[k]
#webctx.session.uid = -1
#web.debug(webctx.session.keys())
else:
web.debug("Reusing session ...")
webctx.session = web.config._session
"""
try:
webctx.session["uid"]
except:
webctx.session = session_default
"""
#web.debug("session.uid: %s" % webctx.session.uid)
#return webctx.session
## main function ###############################################################
"""
try:
webctx.session
except NameError:
web.debug("Resetting session ...")
webctx.session = None
"""
app = None
if __name__ == "__main__":
web.config.debug = False
app = service(urls, globals())
# session setup, make sure to call it only once if in debug mode
init_session(app)
app.add_processor(web.loadhook(hooks.load))
app.add_processor(web.unloadhook(hooks.unload))
#webctx.session["pid"] += 1
#print "starting ..."
#app.add_processor(web.loadhook(loadhook))
#app.add_processor(web.unloadhook(unloadhook))
#app.run(config.port, "0.0.0.0")
#webctx.session = get_session(app)
app.run(config.port, "0.0.0.0", Log)