mirror of
https://github.com/clearlinux/uwsgi.git
synced 2026-10-04 16:08:31 +00:00
Compare commits
336
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
720da09631 | ||
|
|
47f7c09b97 | ||
|
|
cb5704da10 | ||
|
|
c64356b231 | ||
|
|
2acd2f5224 | ||
|
|
ba251f8e3d | ||
|
|
48019f9044 | ||
|
|
d9a1f2448c | ||
|
|
553a947c2d | ||
|
|
736aedfa89 | ||
|
|
6184acc727 | ||
|
|
f0d96b03fa | ||
|
|
14bf840eef | ||
|
|
7ea08093c7 | ||
|
|
8eb8d07151 | ||
|
|
cdd95b434e | ||
|
|
82cbcea886 | ||
|
|
ea54a05fc3 | ||
|
|
eba39dcd67 | ||
|
|
d71ea1404d | ||
|
|
0f0d5b2a5f | ||
|
|
10a75ef848 | ||
|
|
4bd9bf8917 | ||
|
|
323da1299e | ||
|
|
75d08898b4 | ||
|
|
5e47796a02 | ||
|
|
0ade5aba0a | ||
|
|
f7b1ad8cb0 | ||
|
|
3bd147e687 | ||
|
|
66c99e257f | ||
|
|
8407a5921b | ||
|
|
5f36987827 | ||
|
|
ede1622368 | ||
|
|
a28dfb790e | ||
|
|
3711cbbd61 | ||
|
|
c9ef6281ff | ||
|
|
604ea6fd57 | ||
|
|
41dfcaa705 | ||
|
|
daa41d4a51 | ||
|
|
ded4450a70 | ||
|
|
cd0be56bbe | ||
|
|
0bf7b64b46 | ||
|
|
215d2e7055 | ||
|
|
2b5b0cf1bc | ||
|
|
67231da4a9 | ||
|
|
8c25b38470 | ||
|
|
08bbe0cd30 | ||
|
|
9f9f63fe2f | ||
|
|
997bd63cc3 | ||
|
|
6a566d9dca | ||
|
|
d2e743a800 | ||
|
|
2fedb175b4 | ||
|
|
6c5e25dbfe | ||
|
|
94fd3f0bfd | ||
|
|
ca169b05fb | ||
|
|
10ea1fee6e | ||
|
|
d7b8704165 | ||
|
|
0f774b840c | ||
|
|
81521a314b | ||
|
|
37ee7f8908 | ||
|
|
309648f68b | ||
|
|
5cfa65d04e | ||
|
|
e73c8194a0 | ||
|
|
00a3f6ae7f | ||
|
|
b7edaa68c8 | ||
|
|
2877ba54de | ||
|
|
f74a19a71a | ||
|
|
3da5238ee5 | ||
|
|
567dd36592 | ||
|
|
b48062a026 | ||
|
|
b039cb713f | ||
|
|
8e987ffa18 | ||
|
|
52502342e0 | ||
|
|
cb9e3c22f9 | ||
|
|
0c465775ad | ||
|
|
a5417f4963 | ||
|
|
b48076f6e4 | ||
|
|
c472eae332 | ||
|
|
918fc654f4 | ||
|
|
9097cbe10d | ||
|
|
e260873338 | ||
|
|
a7822742c0 | ||
|
|
f1316ef4bb | ||
|
|
4106931175 | ||
|
|
fb5ba40f0c | ||
|
|
854e51f0ad | ||
|
|
676b43f17d | ||
|
|
48b68e6a92 | ||
|
|
61f33721e1 | ||
|
|
5c3c46598f | ||
|
|
4358036f9e | ||
|
|
54d75d92ef | ||
|
|
b1ae57c932 | ||
|
|
2636baa33f | ||
|
|
926c739517 | ||
|
|
79fa25c4e0 | ||
|
|
02050675a8 | ||
|
|
0d5857a1bc | ||
|
|
e3a51be1f9 | ||
|
|
57cb098a6e | ||
|
|
5f285eef56 | ||
|
|
7d5cf6f318 | ||
|
|
99d305c7a1 | ||
|
|
b9cc3b6a7d | ||
|
|
e1f8c69d3d | ||
|
|
67b6fbd15b | ||
|
|
50f407eb9e | ||
|
|
b127699c0a | ||
|
|
4d74fa6759 | ||
|
|
2b9d6cb7c2 | ||
|
|
e0b67bba58 | ||
|
|
3cdf17df75 | ||
|
|
43c086494b | ||
|
|
ae26e90b3a | ||
|
|
d35a7d8725 | ||
|
|
043e7ddde4 | ||
|
|
3930960159 | ||
|
|
be12175c8a | ||
|
|
02a1847539 | ||
|
|
0235535658 | ||
|
|
3af75ca64c | ||
|
|
efbe28502d | ||
|
|
47847ee471 | ||
|
|
688ffe32a9 | ||
|
|
c6bbe01f3e | ||
|
|
002af6128c | ||
|
|
5b5c92a9d7 | ||
|
|
7d31d73fc5 | ||
|
|
c5c2c83c65 | ||
|
|
174116c48b | ||
|
|
4be5830fc3 | ||
|
|
5f55357e17 | ||
|
|
8d033159b8 | ||
|
|
299fd9cf8a | ||
|
|
8caee1b15b | ||
|
|
c2d963aa0d | ||
|
|
94691b3cc3 | ||
|
|
9073a2856b | ||
|
|
4c26f34239 | ||
|
|
ad0cc774e1 | ||
|
|
d238fc50d0 | ||
|
|
dcf16c994e | ||
|
|
65cc44e611 | ||
|
|
9887177d9a | ||
|
|
6b11777bb3 | ||
|
|
604b412f4a | ||
|
|
1d9da31f18 | ||
|
|
fadf640f41 | ||
|
|
743432a53d | ||
|
|
c1956fc371 | ||
|
|
3d098358da | ||
|
|
757e908372 | ||
|
|
dd12caa6f3 | ||
|
|
0bfa5c4f46 | ||
|
|
cb252e4f0a | ||
|
|
bdab4dcf84 | ||
|
|
474860cd39 | ||
|
|
82f722b1f9 | ||
|
|
919501e92c | ||
|
|
b1f3e48b2b | ||
|
|
314df1d809 | ||
|
|
0416914951 | ||
|
|
159a5660b2 | ||
|
|
6ea9ee4ee9 | ||
|
|
c60a81db47 | ||
|
|
b71634c76b | ||
|
|
9a23c42262 | ||
|
|
680df4cff6 | ||
|
|
27fa8df9e6 | ||
|
|
2151b17aa3 | ||
|
|
b93d0cc2b0 | ||
|
|
1729275f8c | ||
|
|
9b2460f8df | ||
|
|
5451ff490c | ||
|
|
5a828aefdc | ||
|
|
6a4332ca4d | ||
|
|
7f3750b174 | ||
|
|
7b1672497e | ||
|
|
113223567a | ||
|
|
ecf23f93d1 | ||
|
|
2504af4dd1 | ||
|
|
c340e972ad | ||
|
|
e382de291f | ||
|
|
51d97fadd9 | ||
|
|
878b515e23 | ||
|
|
03bdaa50b6 | ||
|
|
34dbfc597e | ||
|
|
05efe4b7b3 | ||
|
|
a18bc5f148 | ||
|
|
9852b2fa62 | ||
|
|
9cedbb6716 | ||
|
|
96a6c5eff4 | ||
|
|
e5233f09cc | ||
|
|
2f7bf001e8 | ||
|
|
1caea3dbeb | ||
|
|
6805e5ee9f | ||
|
|
82f33591ae | ||
|
|
b3e3e2ca1d | ||
|
|
4ae0361f6d | ||
|
|
5706dd0a31 | ||
|
|
d8cd5580b7 | ||
|
|
e16c9b46ed | ||
|
|
86d28b2654 | ||
|
|
09bc058e88 | ||
|
|
0bf55fc1f9 | ||
|
|
e05edd9f82 | ||
|
|
aa9c5331d4 | ||
|
|
665511e6fa | ||
|
|
41190a5336 | ||
|
|
f2b72814a6 | ||
|
|
67d49defb6 | ||
|
|
bf1f800cf4 | ||
|
|
55a95ac4b4 | ||
|
|
820caa5364 | ||
|
|
67ce14dfa8 | ||
|
|
dc16e8eb38 | ||
|
|
fc0de78d88 | ||
|
|
1ffecc7939 | ||
|
|
2b4759e5e1 | ||
|
|
779ab3702a | ||
|
|
25fe41dd50 | ||
|
|
1c03935b76 | ||
|
|
6b10370c0a | ||
|
|
e84c3808e9 | ||
|
|
b9ff3f87d0 | ||
|
|
7a6d40c258 | ||
|
|
5c211817b7 | ||
|
|
21b2769edd | ||
|
|
baff440f18 | ||
|
|
793dcd92d2 | ||
|
|
1d203021c7 | ||
|
|
d38cc66738 | ||
|
|
603351c736 | ||
|
|
ae5b69b19c | ||
|
|
0229a9727e | ||
|
|
da571c2515 | ||
|
|
d3160c3f60 | ||
|
|
175ef65d9f | ||
|
|
8a572a642e | ||
|
|
70a51c2909 | ||
|
|
8b32564b05 | ||
|
|
c0ada5e52a | ||
|
|
0ffcb9e05e | ||
|
|
5a279bb170 | ||
|
|
757049d9da | ||
|
|
8674787692 | ||
|
|
3654016c53 | ||
|
|
4dbc15a253 | ||
|
|
3d55ab8327 | ||
|
|
f4e336026b | ||
|
|
ed90e0cf4b | ||
|
|
bed7682a3b | ||
|
|
3874d3e610 | ||
|
|
d378d42c4c | ||
|
|
4faeb852c6 | ||
|
|
27177a4df9 | ||
|
|
4ee9b36579 | ||
|
|
c06a1626ed | ||
|
|
86787b796a | ||
|
|
3bae35805b | ||
|
|
f26e79fb09 | ||
|
|
b72fc0a178 | ||
|
|
f1853568f9 | ||
|
|
2785cd3065 | ||
|
|
a8a497e431 | ||
|
|
710588fcfd | ||
|
|
a66aad02d8 | ||
|
|
651dacad3a | ||
|
|
fdeb2e26cf | ||
|
|
d5d5b87627 | ||
|
|
67c1a547af | ||
|
|
ed9c99c417 | ||
|
|
2e29b6b234 | ||
|
|
e0882ed8df | ||
|
|
a6b42af85a | ||
|
|
595e99a0c6 | ||
|
|
54f666f214 | ||
|
|
f1fad65b43 | ||
|
|
40fb6ffa91 | ||
|
|
d83a685d01 | ||
|
|
e7b346d3d9 | ||
|
|
25ec7aadc0 | ||
|
|
f7ef3161e4 | ||
|
|
2cd731f36d | ||
|
|
15a0b7fbc9 | ||
|
|
f33d16edb7 | ||
|
|
6fdb5cac37 | ||
|
|
20f7e4f0bc | ||
|
|
0c1e9080af | ||
|
|
1ceb82af59 | ||
|
|
ace76d686d | ||
|
|
a7bb9bd2bb | ||
|
|
aa39cc0eca | ||
|
|
74c88f21a9 | ||
|
|
c03e94f97e | ||
|
|
be4da2e8d9 | ||
|
|
d781a02eed | ||
|
|
fd42997e8f | ||
|
|
6be13c2e04 | ||
|
|
63f63adda6 | ||
|
|
c0904e42fc | ||
|
|
8b8f1d60ed | ||
|
|
5a6850e64b | ||
|
|
cc6e9924b8 | ||
|
|
816998155e | ||
|
|
b2ffd406e3 | ||
|
|
713ffe38b4 | ||
|
|
1810af7b2d | ||
|
|
d0fb89c783 | ||
|
|
b0c23faeeb | ||
|
|
edc4aa42c8 | ||
|
|
dda8c2ffaa | ||
|
|
7059fd8d88 | ||
|
|
dad81ce7bd | ||
|
|
277afccf64 | ||
|
|
3854d14867 | ||
|
|
e3a67a9bcd | ||
|
|
a51b19669c | ||
|
|
2707860bc5 | ||
|
|
7c69aade57 | ||
|
|
38c5d3d4b5 | ||
|
|
065b992bf9 | ||
|
|
3b93696775 | ||
|
|
62534b2490 | ||
|
|
5018c152e9 | ||
|
|
57fc6bcef0 | ||
|
|
36f26dba85 | ||
|
|
6062bf8a72 | ||
|
|
0152fb077b | ||
|
|
440d42303d | ||
|
|
72e28580a6 | ||
|
|
acf7984eb3 | ||
|
|
bda58ad5cd | ||
|
|
89258474ad | ||
|
|
dc01fc4b42 | ||
|
|
0dd5add615 |
@@ -0,0 +1,7 @@
|
||||
d3850b52334a91d005b0fe45e1e38ba0997a1fb8 no_server mode
|
||||
2d8a617299153ed2efa6ecee7920e9d36c29e0f5 0.9.5beta1
|
||||
04722a2d0121080d14a64df097c278860db5ce8a 0.9.5rc1
|
||||
820cc64ab296dc4589ef72a33a6b804c22a0430c 0.9.5rc2
|
||||
b0db39cfad5ac6d4f00bbb3f9c57cf73206e2afb 0.9.5
|
||||
9f8b98ad1dfa51b86b13fa624c65f73523075fc4 0.9.5.1
|
||||
a83e7a482e65befcfbb0e24af8cd5d15190acdd7 0.9.6-rc1
|
||||
@@ -1,3 +1,107 @@
|
||||
*** june 2010 ***
|
||||
|
||||
* 0.9.5.3 [20100628]
|
||||
|
||||
- fixed linux x86_64 compilation (sorry)
|
||||
|
||||
* 0.9.5.2 [20100626] *
|
||||
|
||||
- fixed a request parsing bug
|
||||
- applied debian fixes
|
||||
- fixed multiple interpreter sys.argv
|
||||
- improved --chmod-socket
|
||||
- fixed uid 0 SIGHUP
|
||||
- updated nginx and apache modules
|
||||
- updated PSGI, lua and example plugin
|
||||
- fixed --pyargv with python 3.x
|
||||
- improved plugin build system
|
||||
|
||||
*** may 2010 ***
|
||||
|
||||
* 0.9.5.1 [20100519] *
|
||||
|
||||
- fixed paste support
|
||||
- added a new modifier (22)
|
||||
- full support for configuration via environment variables
|
||||
- nginx_string option in nginx module
|
||||
- fix for harakiri without master
|
||||
- backported unladen swallow support for 0.9.6-dev
|
||||
- fixed address space limit detection via getrlimit()
|
||||
- http body buffering implementation
|
||||
- added --prio option (to set priority)
|
||||
- added --post-limit to limit the size of uploads
|
||||
- added --logto option to allow udp logging without daemonizing
|
||||
- applied Gentoo's packagers fixes for build system
|
||||
|
||||
* 0.9.5 [20100501] *
|
||||
|
||||
- hook based request/modifier management
|
||||
- plugin support via dlopen
|
||||
- on-the-fly management flag
|
||||
- integrated proxy
|
||||
- logging via udp
|
||||
- improved spooler for cron-like apps
|
||||
- async support
|
||||
- green thread platform (uGreen) on top of the async mode
|
||||
- transparent Erlang integration
|
||||
- embedded snmp agent
|
||||
- nagios mode
|
||||
- improved python 3.x support
|
||||
- improved xml configuration
|
||||
- new build system
|
||||
- address space usage limiting
|
||||
- lot of portability fixes
|
||||
- lot of optimizations and code refactoring
|
||||
|
||||
|
||||
*** april 2010 ***
|
||||
|
||||
* Fourth Maintenance release [20100427] *
|
||||
|
||||
- backported non-yielding app optimization from 0.9.5
|
||||
- backported udp logging from 0.9.5
|
||||
- backported uwsgi_error() from 0.9.5 (no more dumb perror())
|
||||
- fix a rare (but possible) segmentation fault
|
||||
- added --version
|
||||
- updated apache2 module
|
||||
|
||||
*** march 2010 ***
|
||||
|
||||
* Third Maintenance release [20100310] *
|
||||
|
||||
- fix sendfile on 64bit
|
||||
- include setup.py
|
||||
|
||||
*** february 2010 ***
|
||||
* Maintenance release *
|
||||
|
||||
- better support for NetBSD
|
||||
- threading fix for FreeBSD
|
||||
- sendfile portability fixes
|
||||
- new makefiles
|
||||
- example app for testing static file serving
|
||||
- fixes for ROCK_SOLID mode
|
||||
|
||||
*** january 2010 ***
|
||||
|
||||
- Integrated Spooler
|
||||
- better error management
|
||||
- long options
|
||||
- clustering functions
|
||||
- experimental lighttpd module
|
||||
- official support for OpenBSD and OpenSolaris
|
||||
- FastFuncs
|
||||
- new functions for embedded module
|
||||
- reliable harakiri
|
||||
- better Makefiles
|
||||
- python threads support in master process
|
||||
- proper timeout handling in apache2 module
|
||||
- endianess fixes all over the place
|
||||
- paste support
|
||||
- Django app for managing and monitoring the uWSGI server
|
||||
- options for changin uid/gid and chrooting when run by root
|
||||
- lot of bugfixes and optimizations
|
||||
|
||||
*** 20091129 Version 0.9.3 ***
|
||||
|
||||
- Nginx 0.7.x module
|
||||
|
||||
@@ -1,13 +1,2 @@
|
||||
CC=gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.5-config --cflags`
|
||||
PYTHON_LIBS=`python2.5-config --libs`
|
||||
XML_CFLAGS=`xml2-config --cflags`
|
||||
XML_LIBS=`xml2-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) $(XML_CFLAGS)
|
||||
LD_FLAGS=$(PYTHON_LIBS) $(XML_LIBS)
|
||||
|
||||
PROGRAM=uwsgi
|
||||
|
||||
include base.mk
|
||||
all:
|
||||
python uwsgiconfig.py --build
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
CC=/usr/lib/llvm/llvm/gcc-4.2/bin/llvm-gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.5-config --cflags`
|
||||
PYTHON_LIBS=`python2.5-config --libs`
|
||||
XML_CFLAGS=`xml2-config --cflags`
|
||||
XML_LIBS=`xml2-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) $(XML_CFLAGS)
|
||||
LD_FLAGS=$(PYTHON_LIBS) $(XML_LIBS)
|
||||
|
||||
PROGRAM=uwsgi
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
@@ -1,31 +0,0 @@
|
||||
CC=/usr/lib/llvm/llvm/gcc-4.2/bin/llvm-gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.6-config --cflags`
|
||||
PYTHON_LIBS=`python2.6-config --libs`
|
||||
XML_CFLAGS=`xml2-config --cflags`
|
||||
XML_LIBS=`xml2-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) $(XML_CFLAGS)
|
||||
LD_FLAGS=$(PYTHON_LIBS) $(XML_LIBS)
|
||||
|
||||
PROGRAM=uwsgi26
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
@@ -0,0 +1,2 @@
|
||||
all:
|
||||
python2.4 uwsgiconfig.py --build
|
||||
@@ -0,0 +1,2 @@
|
||||
all:
|
||||
python2.5 uwsgiconfig.py --build
|
||||
+2
-13
@@ -1,13 +1,2 @@
|
||||
CC=gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.6-config --cflags`
|
||||
PYTHON_LIBS=`python2.6-config --libs`
|
||||
XML_CFLAGS=`xml2-config --cflags`
|
||||
XML_LIBS=`xml2-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) $(XML_CFLAGS)
|
||||
LD_FLAGS=$(PYTHON_LIBS) $(XML_LIBS)
|
||||
|
||||
PROGRAM=uwsgi26
|
||||
|
||||
include base.mk
|
||||
all:
|
||||
python2.6 uwsgiconfig.py --build
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
all:
|
||||
python2.7 uwsgiconfig.py --build
|
||||
@@ -0,0 +1,2 @@
|
||||
all:
|
||||
python3.0 uwsgiconfig.py --build
|
||||
@@ -0,0 +1,2 @@
|
||||
all:
|
||||
python3.1 uwsgiconfig.py --build
|
||||
@@ -1,29 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.6-config --cflags`
|
||||
PYTHON_LIBS=`python2.6-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DROCK_SOLID
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DROCK_SOLID
|
||||
|
||||
PROGRAM=uwsgi26_rs
|
||||
|
||||
all: uwsgi
|
||||
|
||||
uwsgi: utils.o socket.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o socket.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o socket.o pymodule.o main.o
|
||||
@@ -1,26 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.4-config --cflags`
|
||||
PYTHON_LIBS=`python2.4-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DUNBIT -D_GNU_SOURCE
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DUNBIT -D_GNU_SOURCE
|
||||
|
||||
PROGRAM=/usr/share/unbit/uwsgi24
|
||||
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) main.o utils.o pymodule.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o pymodule.o main.o
|
||||
@@ -1,26 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYTHON_CFLAGS=`python2.5-config --cflags`
|
||||
PYTHON_LIBS=`python2.5-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DUNBIT -D_GNU_SOURCE
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DUNBIT -D_GNU_SOURCE
|
||||
|
||||
PROGRAM=/usr/share/unbit/uwsgi
|
||||
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) main.o utils.o pymodule.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o pymodule.o main.o
|
||||
@@ -1,33 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYBASE=/opt/packages/python/26
|
||||
|
||||
PATH := ${PATH}:$(PYBASE)/bin
|
||||
|
||||
LD_LIBRARY_PATH= $(PYBASE)/lib
|
||||
export LD_LIBRARY_PATH
|
||||
|
||||
PYTHON_CFLAGS=`python2.6-config --cflags`
|
||||
PYTHON_LIBS=`python2.6-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DUNBIT -D_GNU_SOURCE
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DUNBIT -D_GNU_SOURCE -L $(LD_LIBRARY_PATH)
|
||||
|
||||
PROGRAM=/usr/share/unbit/uwsgi26
|
||||
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o pymodule.o main.o
|
||||
$(CC) $(LD_FLAGS) main.o utils.o pymodule.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o pymodule.o main.o
|
||||
@@ -1,30 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYBASE=/opt/packages/python/30
|
||||
|
||||
PATH := ${PATH}:$(PYBASE)/bin
|
||||
|
||||
LD_LIBRARY_PATH= $(PYBASE)/lib
|
||||
export LD_LIBRARY_PATH
|
||||
|
||||
PYTHON_CFLAGS=`python3.0-config --cflags`
|
||||
PYTHON_LIBS=`python3.0-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DUNBIT -D_GNU_SOURCE -DPYTHREE
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DUNBIT -D_GNU_SOURCE -DPYTHREE -L $(LD_LIBRARY_PATH)
|
||||
|
||||
PROGRAM=/usr/share/unbit/uwsgi30
|
||||
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o main.o
|
||||
$(CC) $(LD_FLAGS) main.o utils.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o main.o
|
||||
@@ -1,30 +0,0 @@
|
||||
CC=gcc
|
||||
|
||||
PYBASE=/opt/packages/python/31
|
||||
|
||||
PATH := ${PATH}:$(PYBASE)/bin
|
||||
|
||||
LD_LIBRARY_PATH= $(PYBASE)/lib
|
||||
export LD_LIBRARY_PATH
|
||||
|
||||
PYTHON_CFLAGS=`python3.1-config --cflags`
|
||||
PYTHON_LIBS=`python3.1-config --libs`
|
||||
|
||||
CFLAGS=$(PYTHON_CFLAGS) -DUNBIT -D_GNU_SOURCE -DPYTHREE
|
||||
LD_FLAGS=$(PYTHON_LIBS) -DUNBIT -D_GNU_SOURCE -DPYTHREE -L $(LD_LIBRARY_PATH)
|
||||
|
||||
PROGRAM=/usr/share/unbit/uwsgi31
|
||||
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o main.o
|
||||
$(CC) $(LD_FLAGS) main.o utils.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o main.o
|
||||
@@ -0,0 +1,923 @@
|
||||
/*
|
||||
|
||||
*** uWSGI/mod_Ruwsgi ***
|
||||
|
||||
Copyright 2009-2010 Roger Florkowski <rflorkowski@mypublisher.com>
|
||||
Copyright 2009-2010 Unbit S.a.s. <info@unbit.it>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
#define MOD_UWSGI_VERSION "1.0"
|
||||
|
||||
#include "ap_config.h"
|
||||
#include "apr_version.h"
|
||||
#include "apr_lib.h"
|
||||
#include "apr_strings.h"
|
||||
#include "httpd.h"
|
||||
#include "http_core.h"
|
||||
#include "http_request.h"
|
||||
#include "http_config.h"
|
||||
#include "http_log.h"
|
||||
#include "http_protocol.h"
|
||||
#include "util_script.h"
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/uio.h>
|
||||
#include <time.h>
|
||||
#include <poll.h>
|
||||
|
||||
#define DEFAULT_TIMEOUT 60 /* default socket timeout */
|
||||
#define DEFAULT_CGIMODE 0 /* not enabled */
|
||||
|
||||
#define UNSET 0
|
||||
#define ENABLED 1
|
||||
#define DISABLED 2
|
||||
|
||||
#if APR_MAJOR_VERSION == 0
|
||||
#define apr_socket_send apr_send
|
||||
#define GET_PORT(port, addr) apr_sockaddr_port_get(&(port), addr)
|
||||
#define CREATE_SOCKET(sock, family, pool) \
|
||||
apr_socket_create(sock, family, SOCK_STREAM, pool)
|
||||
#else
|
||||
#define GET_PORT(port, addr) ((port) = (addr)->port)
|
||||
#define CREATE_SOCKET(sock, family, pool) \
|
||||
apr_socket_create(sock, family, SOCK_STREAM, APR_PROTO_TCP, pool)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
char *path;
|
||||
char *addr;
|
||||
apr_port_t port;
|
||||
} mount_entry;
|
||||
|
||||
/*
|
||||
* Configuration record. Used per-directory configuration data.
|
||||
*/
|
||||
typedef struct {
|
||||
mount_entry mount;
|
||||
int enabled; /* mod_uwsgi is enabled from this directory */
|
||||
int timeout;
|
||||
int cgi_mode;
|
||||
uint8_t modifier1;
|
||||
uint8_t modifier2;
|
||||
} uwsgi_cfg;
|
||||
|
||||
/* Server level configuration */
|
||||
typedef struct {
|
||||
apr_array_header_t *mounts;
|
||||
int timeout;
|
||||
int cgi_mode;
|
||||
} uwsgi_server_cfg;
|
||||
|
||||
module AP_MODULE_DECLARE_DATA uwsgi_module;
|
||||
|
||||
/*
|
||||
* Locate our directory configuration record for the current request.
|
||||
*/
|
||||
static uwsgi_cfg *
|
||||
our_dconfig(request_rec *r)
|
||||
{
|
||||
return (uwsgi_cfg *) ap_get_module_config(r->per_dir_config, &uwsgi_module);
|
||||
}
|
||||
|
||||
static uwsgi_server_cfg *our_sconfig(server_rec *s)
|
||||
{
|
||||
return (uwsgi_server_cfg *) ap_get_module_config(s->module_config,
|
||||
&uwsgi_module);
|
||||
}
|
||||
|
||||
static int
|
||||
mount_entry_matches(const char *url, const char *prefix,
|
||||
const char **path_info)
|
||||
{
|
||||
int i;
|
||||
for (i=0; prefix[i] != '\0'; i++) {
|
||||
if (url[i] == '\0' || url[i] != prefix[i])
|
||||
return 0;
|
||||
}
|
||||
if (url[i] == '\0' || url[i] == '/') {
|
||||
*path_info = url + i;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int uwsgi_translate(request_rec *r)
|
||||
{
|
||||
uwsgi_cfg *cfg = our_dconfig(r);
|
||||
|
||||
if (cfg->enabled == DISABLED) {
|
||||
return DECLINED;
|
||||
}
|
||||
|
||||
if (cfg->mount.addr != UNSET) {
|
||||
ap_assert(cfg->mount.port != UNSET);
|
||||
r->handler = "uwsgi-handler";
|
||||
r->filename = r->uri;
|
||||
return OK;
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
uwsgi_server_cfg *scfg = our_sconfig(r->server);
|
||||
mount_entry *entries = (mount_entry *) scfg->mounts->elts;
|
||||
for (i = 0; i < scfg->mounts->nelts; ++i) {
|
||||
const char *path_info;
|
||||
mount_entry *mount = &entries[i];
|
||||
if (mount_entry_matches(r->uri, mount->path, &path_info)) {
|
||||
r->handler = "uwsgi-handler";
|
||||
r->path_info = apr_pstrdup(r->pool, path_info);
|
||||
r->filename = r->uri;
|
||||
ap_set_module_config(r->request_config, &uwsgi_module, mount);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
return DECLINED;
|
||||
}
|
||||
|
||||
static int uwsgi_map_location(request_rec *r)
|
||||
{
|
||||
if (r->handler && strcmp(r->handler, "uwsgi-handler") == 0) {
|
||||
return OK; /* We don't want directory walk. */
|
||||
}
|
||||
return DECLINED;
|
||||
}
|
||||
|
||||
static void log_err(const char *file, int line, request_rec *r,
|
||||
apr_status_t status, const char *msg)
|
||||
{
|
||||
ap_log_rerror(file, line, APLOG_ERR, status, r, "uwsgi: %s", msg);
|
||||
}
|
||||
|
||||
static void log_debug(const char *file, int line, request_rec *r, const
|
||||
char *msg)
|
||||
{
|
||||
/*
|
||||
ap_log_rerror(file, line, APLOG_DEBUG, APR_SUCCESS, r, msg);
|
||||
ap_log_rerror(file, line, APLOG_WARNING, APR_SUCCESS, r, "uwsgi: %s", msg);
|
||||
*/
|
||||
ap_log_rerror(file, line, APLOG_DEBUG, APR_SUCCESS, r, "uwsgi: %s", msg);
|
||||
}
|
||||
|
||||
/* buffered socket implementation (buckets are overkill) */
|
||||
|
||||
#define BUFFER_SIZE 8000
|
||||
|
||||
struct sockbuff {
|
||||
apr_socket_t *sock;
|
||||
char buf[BUFFER_SIZE];
|
||||
int used;
|
||||
};
|
||||
|
||||
static void binit(struct sockbuff *s, apr_socket_t *sock)
|
||||
{
|
||||
s->sock = sock;
|
||||
s->used = 0;
|
||||
}
|
||||
|
||||
static apr_status_t sendall(apr_socket_t *sock, char *buf, apr_size_t len)
|
||||
{
|
||||
apr_status_t rv;
|
||||
apr_size_t n;
|
||||
while (len > 0) {
|
||||
n = len;
|
||||
if ((rv = apr_socket_send(sock, buf, &n))) return rv;
|
||||
buf += n;
|
||||
len -= n;
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static apr_status_t bflush(struct sockbuff *s)
|
||||
{
|
||||
apr_status_t rv;
|
||||
ap_assert(s->used >= 0 && s->used <= BUFFER_SIZE);
|
||||
if (s->used) {
|
||||
if ((rv = sendall(s->sock, s->buf, s->used))) return rv;
|
||||
s->used = 0;
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static apr_status_t bwrite(struct sockbuff *s, char *buf, apr_size_t len)
|
||||
{
|
||||
apr_status_t rv;
|
||||
if (len >= BUFFER_SIZE - s->used) {
|
||||
if ((rv = bflush(s))) return rv;
|
||||
while (len >= BUFFER_SIZE) {
|
||||
if ((rv = sendall(s->sock, buf, BUFFER_SIZE))) return rv;
|
||||
buf += BUFFER_SIZE;
|
||||
len -= BUFFER_SIZE;
|
||||
}
|
||||
}
|
||||
if (len > 0) {
|
||||
ap_assert(len < BUFFER_SIZE - s->used);
|
||||
memcpy(s->buf + s->used, buf, len);
|
||||
s->used += len;
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static apr_status_t bputs(struct sockbuff *s, char *buf)
|
||||
{
|
||||
return bwrite(s, buf, strlen(buf));
|
||||
}
|
||||
|
||||
static apr_status_t bputc(struct sockbuff *s, char c)
|
||||
{
|
||||
char buf[1];
|
||||
buf[0] = c;
|
||||
return bwrite(s, buf, 1);
|
||||
}
|
||||
|
||||
static apr_status_t bputh(struct sockbuff *s, unsigned short h)
|
||||
{
|
||||
return bwrite(s, (char *)&h, 2);
|
||||
}
|
||||
|
||||
#define CONFIG_VALUE(value, fallback) ((value) != UNSET ? (value) : (fallback))
|
||||
|
||||
static apr_status_t
|
||||
open_socket(apr_socket_t **sock, request_rec *r)
|
||||
{
|
||||
int timeout;
|
||||
int retries = 4;
|
||||
int sleeptime = 1;
|
||||
apr_status_t rv;
|
||||
apr_sockaddr_t *sockaddr;
|
||||
uwsgi_server_cfg *scfg = our_sconfig(r->server);
|
||||
uwsgi_cfg *cfg = our_dconfig(r);
|
||||
mount_entry *m = (mount_entry *) ap_get_module_config(r->request_config,
|
||||
&uwsgi_module);
|
||||
if (!m) {
|
||||
m = &cfg->mount;
|
||||
}
|
||||
|
||||
timeout = CONFIG_VALUE(cfg->timeout, CONFIG_VALUE(scfg->timeout,
|
||||
DEFAULT_TIMEOUT));
|
||||
rv = apr_sockaddr_info_get(&sockaddr,
|
||||
CONFIG_VALUE(m->addr, "localhost"),
|
||||
APR_UNSPEC,
|
||||
CONFIG_VALUE(m->port, 5000),
|
||||
0,
|
||||
r->pool);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "apr_sockaddr_info_get() error");
|
||||
return rv;
|
||||
}
|
||||
|
||||
restart:
|
||||
*sock = NULL;
|
||||
rv = CREATE_SOCKET(sock, sockaddr->family, r->pool);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "apr_socket_create() error");
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = apr_socket_timeout_set(*sock, apr_time_from_sec(timeout));
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "apr_socket_timeout_set() error");
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = apr_socket_connect(*sock, sockaddr);
|
||||
if (rv) {
|
||||
apr_socket_close(*sock);
|
||||
if ((APR_STATUS_IS_ECONNREFUSED(rv) |
|
||||
APR_STATUS_IS_EINPROGRESS(rv)) && retries > 0) {
|
||||
/* server may be temporarily down, retry */
|
||||
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_DEBUG, rv, r,
|
||||
"uwsgi: connection failed, retrying");
|
||||
apr_sleep(apr_time_from_sec(sleeptime));
|
||||
--retries;
|
||||
sleeptime *= 2;
|
||||
goto restart;
|
||||
}
|
||||
log_err(APLOG_MARK, r, rv, "uwsgi: can't connect to server");
|
||||
return rv;
|
||||
}
|
||||
|
||||
#ifdef APR_TCP_NODELAY
|
||||
/* disable Nagle, we don't send small packets */
|
||||
apr_socket_opt_set(*sock, APR_TCP_NODELAY, 1);
|
||||
#endif
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
/* This code is a duplicate of what's in util_script.c. We can't use
|
||||
* r->unparsed_uri because it gets changed if there was a redirect. */
|
||||
static char *original_uri(request_rec *r)
|
||||
{
|
||||
char *first, *last;
|
||||
|
||||
if (r->the_request == NULL) {
|
||||
return (char *) apr_pcalloc(r->pool, 1);
|
||||
}
|
||||
|
||||
first = r->the_request; /* use the request-line */
|
||||
|
||||
while (*first && !apr_isspace(*first)) {
|
||||
++first; /* skip over the method */
|
||||
}
|
||||
while (apr_isspace(*first)) {
|
||||
++first; /* and the space(s) */
|
||||
}
|
||||
|
||||
last = first;
|
||||
while (*last && !apr_isspace(*last)) {
|
||||
++last; /* end at next whitespace */
|
||||
}
|
||||
|
||||
return apr_pstrmemdup(r->pool, first, last - first);
|
||||
}
|
||||
|
||||
static char *lookup_name(apr_table_t *t, const char *name)
|
||||
{
|
||||
const apr_array_header_t *hdrs_arr = apr_table_elts(t);
|
||||
apr_table_entry_t *hdrs = (apr_table_entry_t *) hdrs_arr->elts;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < hdrs_arr->nelts; ++i) {
|
||||
if (hdrs[i].key == NULL)
|
||||
continue;
|
||||
|
||||
if (strcasecmp(hdrs[i].key, name) == 0)
|
||||
return hdrs[i].val;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static char *lookup_header(request_rec *r, const char *name)
|
||||
{
|
||||
return lookup_name(r->headers_in, name);
|
||||
}
|
||||
|
||||
static void add_header(apr_table_t *t, const char *name, const char *value)
|
||||
{
|
||||
if (name != NULL && value != NULL)
|
||||
apr_table_addn(t, name, value);
|
||||
}
|
||||
|
||||
static int find_path_info(const char *uri, const char *path_info)
|
||||
{
|
||||
int n;
|
||||
n = strlen(uri) - strlen(path_info);
|
||||
ap_assert(n >= 0);
|
||||
return n;
|
||||
}
|
||||
|
||||
static char *http2env(apr_pool_t *p, const char *name)
|
||||
{
|
||||
char *env_name = apr_pstrcat(p, "HTTP_", name, NULL);
|
||||
char *cp;
|
||||
|
||||
for (cp = env_name + 5; *cp != 0; cp++) {
|
||||
if (*cp == '-') {
|
||||
*cp = '_';
|
||||
}
|
||||
else {
|
||||
*cp = apr_toupper(*cp);
|
||||
}
|
||||
}
|
||||
|
||||
return env_name;
|
||||
}
|
||||
|
||||
static apr_status_t
|
||||
send_headers(request_rec *r, struct sockbuff *s)
|
||||
{
|
||||
int i;
|
||||
const apr_array_header_t *hdrs_arr, *env_arr;
|
||||
apr_table_entry_t *hdrs, *env;
|
||||
char *buf;
|
||||
unsigned short int n = 0;
|
||||
apr_table_t *t;
|
||||
apr_status_t rv = 0;
|
||||
apr_port_t port = 0;
|
||||
GET_PORT(port, r->connection->remote_addr);
|
||||
uwsgi_cfg *cfg = our_dconfig(r);
|
||||
|
||||
log_debug(APLOG_MARK,r, "sending headers");
|
||||
t = apr_table_make(r->pool, 40);
|
||||
if (!t)
|
||||
return APR_ENOMEM;
|
||||
|
||||
/* headers to send */
|
||||
|
||||
/* CONTENT_LENGTH must come first and always be present */
|
||||
buf = lookup_header(r, "Content-Length");
|
||||
if (buf == NULL)
|
||||
buf = "0";
|
||||
add_header(t, "CONTENT_LENGTH", buf);
|
||||
|
||||
add_header(t, "REQUEST_METHOD", (char *) r->method) ;
|
||||
add_header(t, "QUERY_STRING", r->args ? r->args : "") ;
|
||||
add_header(t, "SERVER_SOFTWARE", ap_get_server_version());
|
||||
add_header(t, "SERVER_ADMIN", r->server->server_admin);
|
||||
add_header(t, "SERVER_NAME", (char *) ap_get_server_name(r)) ;
|
||||
add_header(t, "SERVER_PORT", apr_psprintf(r->pool, "%u",ap_get_server_port(r))) ;
|
||||
add_header(t, "SERVER_ADDR", r->connection->local_ip);
|
||||
add_header(t, "SERVER_PROTOCOL", r->protocol) ;
|
||||
|
||||
add_header(t, "REQUEST_URI", original_uri(r)) ;
|
||||
add_header(t, "REMOTE_ADDR", r->connection->remote_ip) ;
|
||||
add_header(t, "REMOTE_PORT", apr_psprintf(r->pool, "%d", port));
|
||||
add_header(t, "REMOTE_USER", r->user) ;
|
||||
add_header(t, "DOCUMENT_ROOT", (char *) ap_document_root(r)) ;
|
||||
|
||||
if (r->path_info) {
|
||||
int path_info_start = find_path_info(r->uri, r->path_info);
|
||||
add_header(t, "SCRIPT_NAME", apr_pstrndup(r->pool, r->uri,
|
||||
path_info_start));
|
||||
add_header(t, "PATH_INFO", r->path_info);
|
||||
}
|
||||
else {
|
||||
/* skip PATH_INFO, don't know it */
|
||||
add_header(t, "SCRIPT_NAME", r->uri) ;
|
||||
}
|
||||
|
||||
add_header(t, "CONTENT_TYPE", lookup_header(r, "Content-type"));
|
||||
|
||||
/* HTTP headers */
|
||||
hdrs_arr = apr_table_elts(r->headers_in);
|
||||
hdrs = (apr_table_entry_t *) hdrs_arr->elts;
|
||||
for (i = 0; i < hdrs_arr->nelts; ++i) {
|
||||
if (hdrs[i].key) {
|
||||
add_header(t, http2env(r->pool, hdrs[i].key), hdrs[i].val);
|
||||
}
|
||||
}
|
||||
|
||||
/* environment variables */
|
||||
env_arr = apr_table_elts(r->subprocess_env);
|
||||
env = (apr_table_entry_t*) env_arr->elts;
|
||||
for (i = 0; i < env_arr->nelts; ++i) {
|
||||
add_header(t, env[i].key, env[i].val);
|
||||
}
|
||||
|
||||
hdrs_arr = apr_table_elts(t);
|
||||
hdrs = (apr_table_entry_t*) hdrs_arr->elts;
|
||||
|
||||
/* calculate length of header data */
|
||||
for (i = 0; i < hdrs_arr->nelts; ++i) {
|
||||
n += strlen(hdrs[i].key);
|
||||
n += strlen(hdrs[i].val);
|
||||
n += 4 ; /* XXX */
|
||||
}
|
||||
log_debug(APLOG_MARK,r, apr_psprintf(r->pool, "pktheader size is %u", n)) ;
|
||||
log_debug(APLOG_MARK,r, apr_psprintf(r->pool, "num headers %u", hdrs_arr->nelts)) ;
|
||||
|
||||
/* write pkt header */
|
||||
rv = bputc(s, cfg->modifier1); /* marker */
|
||||
if (rv) return rv;
|
||||
rv = bputh(s, n); /* length of header data */
|
||||
if (rv) return rv;
|
||||
rv = bputc(s, cfg->modifier2); /* marker */
|
||||
if (rv) return rv;
|
||||
|
||||
/* write out headers */
|
||||
for (i = 0; i < hdrs_arr->nelts; ++i) {
|
||||
rv = bputh(s, strlen(hdrs[i].key)); /* length */
|
||||
if (rv) return rv;
|
||||
rv = bputs(s, hdrs[i].key); /* key */
|
||||
if (rv) return rv;
|
||||
rv = bputh(s, strlen(hdrs[i].val)); /* length */
|
||||
if (rv) return rv;
|
||||
rv = bputs(s, hdrs[i].val); /* data */
|
||||
if (rv) return rv;
|
||||
}
|
||||
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static apr_status_t send_request_body(request_rec *r, struct sockbuff *s)
|
||||
{
|
||||
if (ap_should_client_block(r)) {
|
||||
char buf[BUFFER_SIZE];
|
||||
apr_status_t rv;
|
||||
apr_off_t len;
|
||||
|
||||
while ((len = ap_get_client_block(r, buf, sizeof buf)) > 0) {
|
||||
if ((rv = bwrite(s, buf, len))) return rv;
|
||||
}
|
||||
if (len == -1)
|
||||
return HTTP_INTERNAL_SERVER_ERROR; /* what to return? */
|
||||
}
|
||||
return APR_SUCCESS;
|
||||
}
|
||||
|
||||
static int uwsgi_handler(request_rec *r)
|
||||
{
|
||||
apr_status_t rv = 0;
|
||||
int cgi_mode, http_status = 0;
|
||||
struct sockbuff s;
|
||||
apr_socket_t *sock;
|
||||
apr_bucket_brigade *bb = NULL;
|
||||
apr_bucket *b = NULL;
|
||||
const char *location;
|
||||
uwsgi_cfg *cfg = our_dconfig(r);
|
||||
uwsgi_server_cfg *scfg = our_sconfig(r->server);
|
||||
|
||||
if (strcmp(r->handler, "uwsgi-handler"))
|
||||
return DECLINED;
|
||||
|
||||
http_status = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
|
||||
if (http_status != OK)
|
||||
return http_status;
|
||||
|
||||
log_debug(APLOG_MARK, r, "connecting to server");
|
||||
|
||||
rv = open_socket(&sock, r);
|
||||
if (rv) {
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
binit(&s, sock);
|
||||
|
||||
rv = send_headers(r, &s);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "error sending request headers");
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
rv = send_request_body(r, &s);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "error sending request body");
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
rv = bflush(&s);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "error sending request");
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
|
||||
log_debug(APLOG_MARK, r, "reading response headers");
|
||||
bb = apr_brigade_create(r->connection->pool, r->connection->bucket_alloc);
|
||||
|
||||
cgi_mode = CONFIG_VALUE(cfg->cgi_mode, CONFIG_VALUE(scfg->cgi_mode, DEFAULT_CGIMODE));
|
||||
if (!cgi_mode) {
|
||||
|
||||
/* receive implemented with http-0.9 based protocol */
|
||||
apr_interval_time_t timeout;
|
||||
char buf[4096] ;
|
||||
apr_pollfd_t pollfd = {r->pool, APR_POLL_SOCKET, APR_POLLIN, 0, { NULL }, NULL };
|
||||
pollfd.desc.s = sock;
|
||||
timeout = CONFIG_VALUE(cfg->timeout, CONFIG_VALUE(scfg->timeout,
|
||||
DEFAULT_TIMEOUT)) * 1000 * 1000;
|
||||
r->assbackwards = 1 ; /* XXX */
|
||||
|
||||
for(;;) {
|
||||
apr_int32_t nsds;
|
||||
apr_size_t len;
|
||||
|
||||
if ((rv = apr_poll(&pollfd, 1, &nsds, timeout)) == APR_TIMEUP) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: recv() timeout");
|
||||
break;
|
||||
}
|
||||
if (rv != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: poll() %s", strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
len = 4096;
|
||||
rv = apr_socket_recv(sock, buf, &len);
|
||||
if (rv == APR_SUCCESS) {
|
||||
apr_brigade_write(bb, NULL, NULL, buf, len);
|
||||
}
|
||||
else if (rv == APR_EOF) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: recv() %s", strerror(errno));
|
||||
}
|
||||
|
||||
}
|
||||
log_debug(APLOG_MARK,r, "recv finished") ;
|
||||
b = apr_bucket_flush_create(r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
b = apr_bucket_eos_create(r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
|
||||
} else {
|
||||
|
||||
/* receive implemented with http-1.x based protocol */
|
||||
b = apr_bucket_socket_create(sock, r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
b = apr_bucket_eos_create(r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
|
||||
rv = ap_scan_script_header_err_brigade(r, bb, NULL);
|
||||
if (rv) {
|
||||
if (rv == HTTP_INTERNAL_SERVER_ERROR) {
|
||||
log_err(APLOG_MARK, r, rv, "error reading response headers");
|
||||
}
|
||||
else {
|
||||
/* Work around an Apache bug whereby the returned status is
|
||||
* ignored and status_line is used instead. This bug is
|
||||
* present at least in 2.0.54.
|
||||
*/
|
||||
r->status_line = NULL;
|
||||
}
|
||||
apr_brigade_destroy(bb);
|
||||
return rv;
|
||||
}
|
||||
|
||||
location = apr_table_get(r->headers_out, "Location");
|
||||
|
||||
if (location && location[0] == '/' &&
|
||||
((r->status == HTTP_OK) || ap_is_HTTP_REDIRECT(r->status))) {
|
||||
|
||||
apr_brigade_destroy(bb);
|
||||
|
||||
/* Internal redirect -- fake-up a pseudo-request */
|
||||
r->status = HTTP_OK;
|
||||
|
||||
/* This redirect needs to be a GET no matter what the original
|
||||
* method was.
|
||||
*/
|
||||
r->method = apr_pstrdup(r->pool, "GET");
|
||||
r->method_number = M_GET;
|
||||
|
||||
ap_internal_redirect_handler(location, r);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
rv = ap_pass_brigade(r->output_filters, bb);
|
||||
if (rv) {
|
||||
log_err(APLOG_MARK, r, rv, "ap_pass_brigade()");
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
|
||||
static int uwsgi_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp,
|
||||
server_rec *base_server)
|
||||
{
|
||||
ap_add_version_component(p, "mod_uwsgi/" MOD_UWSGI_VERSION);
|
||||
return OK;
|
||||
}
|
||||
|
||||
static void *
|
||||
uwsgi_create_dir_config(apr_pool_t *p, char *dirspec)
|
||||
{
|
||||
uwsgi_cfg *cfg = apr_pcalloc(p, sizeof(uwsgi_cfg));
|
||||
|
||||
cfg->enabled = UNSET;
|
||||
cfg->mount.addr = UNSET;
|
||||
cfg->mount.port = UNSET;
|
||||
cfg->timeout = UNSET;
|
||||
cfg->cgi_mode = UNSET;
|
||||
cfg->modifier1 = UNSET;
|
||||
cfg->modifier2 = UNSET;
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
#define MERGE(b, n, a) (n->a == UNSET ? b->a : n->a)
|
||||
|
||||
static void *
|
||||
uwsgi_merge_dir_config(apr_pool_t *p, void *basev, void *newv)
|
||||
{
|
||||
uwsgi_cfg* cfg = apr_pcalloc(p, sizeof(uwsgi_cfg));
|
||||
uwsgi_cfg* base = basev;
|
||||
uwsgi_cfg* new = newv;
|
||||
|
||||
cfg->enabled = MERGE(base, new, enabled);
|
||||
cfg->mount.addr = MERGE(base, new, mount.addr);
|
||||
cfg->mount.port = MERGE(base, new, mount.port);
|
||||
cfg->timeout = MERGE(base, new, timeout);
|
||||
cfg->cgi_mode = MERGE(base, new, cgi_mode);
|
||||
cfg->modifier1 = MERGE(base, new, modifier1);
|
||||
cfg->modifier2 = MERGE(base, new, modifier2);
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static void *
|
||||
uwsgi_create_server_config(apr_pool_t *p, server_rec *s)
|
||||
{
|
||||
uwsgi_server_cfg *c =
|
||||
(uwsgi_server_cfg *) apr_pcalloc(p, sizeof(uwsgi_server_cfg));
|
||||
|
||||
c->mounts = apr_array_make(p, 20, sizeof(mount_entry));
|
||||
c->timeout = UNSET;
|
||||
c->cgi_mode = UNSET;
|
||||
return c;
|
||||
}
|
||||
|
||||
static void *
|
||||
uwsgi_merge_server_config(apr_pool_t *p, void *basev, void *overridesv)
|
||||
{
|
||||
uwsgi_server_cfg *c = (uwsgi_server_cfg *)
|
||||
apr_pcalloc(p, sizeof(uwsgi_server_cfg));
|
||||
uwsgi_server_cfg *base = (uwsgi_server_cfg *) basev;
|
||||
uwsgi_server_cfg *overrides = (uwsgi_server_cfg *) overridesv;
|
||||
|
||||
c->mounts = apr_array_append(p, overrides->mounts, base->mounts);
|
||||
c->timeout = MERGE(base, overrides, timeout);
|
||||
c->cgi_mode = MERGE(base, overrides, cgi_mode);
|
||||
return c;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_mount(cmd_parms *cmd, void *dummy, const char *path, const char *addr)
|
||||
{
|
||||
int n;
|
||||
apr_status_t rv;
|
||||
char *scope_id = NULL; /* A ip6 parameter - not used here. */
|
||||
uwsgi_server_cfg *scfg = our_sconfig(cmd->server);
|
||||
mount_entry *new = apr_array_push(scfg->mounts);
|
||||
n = strlen(path);
|
||||
while (n > 0 && path[n-1] == '/') {
|
||||
n--; /* strip trailing slashes */
|
||||
}
|
||||
new->path = apr_pstrndup(cmd->pool, path, n);
|
||||
rv = apr_parse_addr_port(&new->addr, &scope_id, &new->port, addr,
|
||||
cmd->pool);
|
||||
if (rv)
|
||||
return "error parsing address:port string";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_server(cmd_parms *cmd, void *pcfg, const char *addr_and_port)
|
||||
{
|
||||
apr_status_t rv;
|
||||
uwsgi_cfg *cfg = pcfg;
|
||||
char *scope_id = NULL; /* A ip6 parameter - not used here. */
|
||||
|
||||
if (cmd->path == NULL)
|
||||
return "not a server command";
|
||||
|
||||
rv = apr_parse_addr_port(&cfg->mount.addr, &scope_id, &cfg->mount.port,
|
||||
addr_and_port, cmd->pool);
|
||||
if (rv)
|
||||
return "error parsing address:port string";
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_handler(cmd_parms* cmd, void* pcfg, int flag)
|
||||
{
|
||||
uwsgi_cfg *cfg = pcfg;
|
||||
|
||||
if (cmd->path == NULL) /* server command */
|
||||
return "not a server command";
|
||||
|
||||
if (flag)
|
||||
cfg->enabled = ENABLED;
|
||||
else
|
||||
cfg->enabled = DISABLED;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_timeout(cmd_parms *cmd, void* pcfg, const char *strtimeout)
|
||||
{
|
||||
uwsgi_cfg *dcfg = pcfg;
|
||||
int timeout = atoi(strtimeout);
|
||||
|
||||
if (cmd->path == NULL) {
|
||||
uwsgi_server_cfg *scfg = our_sconfig(cmd->server);
|
||||
scfg->timeout = timeout;
|
||||
}
|
||||
else {
|
||||
dcfg->timeout = timeout;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_force_cgi_mode(cmd_parms *cmd, void *pcfg, const char *value)
|
||||
{
|
||||
int cgi_mode;
|
||||
|
||||
if (!strcmp("yes", value) || !strcmp("on", value) || !strcmp("enable", value) || !strcmp("1", value)) {
|
||||
cgi_mode = 1;
|
||||
}
|
||||
else {
|
||||
cgi_mode = 0;
|
||||
}
|
||||
|
||||
if (cmd->path == NULL) {
|
||||
uwsgi_server_cfg *scfg = our_sconfig(cmd->server);
|
||||
scfg->cgi_mode = cgi_mode;
|
||||
}
|
||||
else {
|
||||
uwsgi_cfg *dcfg = pcfg;
|
||||
dcfg->cgi_mode = cgi_mode;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_modifier1(cmd_parms *cmd, void *pcfg, const char *value)
|
||||
{
|
||||
uwsgi_cfg *cfg = pcfg;
|
||||
int val ;
|
||||
|
||||
if (cmd->path == NULL) /* server command */
|
||||
return "not a server command";
|
||||
|
||||
val = atoi(value);
|
||||
if (val < 0 || val > 255) {
|
||||
return "ignored uWSGImodifier1. Value must be between 0 and 255" ;
|
||||
}
|
||||
else {
|
||||
cfg->modifier1 = (uint8_t) val;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *
|
||||
cmd_uwsgi_modifier2(cmd_parms *cmd, void *pcfg, const char *value)
|
||||
{
|
||||
uwsgi_cfg *cfg = pcfg;
|
||||
int val ;
|
||||
|
||||
if (cmd->path == NULL) /* server command */
|
||||
return "not a server command";
|
||||
|
||||
val = atoi(value);
|
||||
if (val < 0 || val > 255) {
|
||||
return "ignored uWSGImodifier2. Value must be between 0 and 255" ;
|
||||
}
|
||||
else {
|
||||
cfg->modifier2 = (uint8_t) val;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const command_rec uwsgi_cmds[] = {
|
||||
AP_INIT_TAKE2("uWSGImount", cmd_uwsgi_mount, NULL, RSRC_CONF,
|
||||
"path prefix and address of UWSGI server"),
|
||||
AP_INIT_TAKE1("uWSGIserver", cmd_uwsgi_server, NULL, ACCESS_CONF,
|
||||
"Address and port of an UWSGI server (e.g. localhost:4000)"),
|
||||
AP_INIT_FLAG( "uWSGIhandler", cmd_uwsgi_handler, NULL, ACCESS_CONF,
|
||||
"On or Off to enable or disable the UWSGI handler"),
|
||||
AP_INIT_TAKE1("uWSGIserverTimeout", cmd_uwsgi_timeout, NULL, ACCESS_CONF|RSRC_CONF,
|
||||
"Timeout (in seconds) for communication with the UWSGI server."),
|
||||
AP_INIT_TAKE1("uWSGIforceCGImode", cmd_uwsgi_force_cgi_mode, NULL, ACCESS_CONF|RSRC_CONF,
|
||||
"Force uWSGI CGI mode for perfect integration with apache filter"),
|
||||
AP_INIT_TAKE1("uWSGImodifier1", cmd_uwsgi_modifier1, NULL, ACCESS_CONF,
|
||||
"Set uWSGI modifier1"),
|
||||
AP_INIT_TAKE1("uWSGImodifier2", cmd_uwsgi_modifier2, NULL, ACCESS_CONF,
|
||||
"Set uWSGI modifier2"),
|
||||
/*
|
||||
AP_INIT_TAKE12("uWSGIsocket", cmd_uwsgi_socket, NULL, RSRC_CONF|ACCESS_CONF,
|
||||
"Absolute path and optional timeout in seconds of uwsgi server socket"),
|
||||
AP_INIT_TAKE1("uWSGIsocket2", cmd_uwsgi_socket2, NULL, RSRC_CONF|ACCESS_CONF,
|
||||
"Absolute path of failover uwsgi server socket"),
|
||||
AP_INIT_TAKE1("uWSGIforceScriptName", cmd_uwsgi_force_script_name, NULL, ACCESS_CONF,
|
||||
"Fix for PATH_INFO/SCRIPT_NAME when the location has filesystem correspondence"),
|
||||
*/
|
||||
{NULL}
|
||||
};
|
||||
|
||||
static void uwsgi_register_hooks(apr_pool_t *p)
|
||||
{
|
||||
ap_hook_post_config(uwsgi_init, NULL, NULL, APR_HOOK_MIDDLE);
|
||||
ap_hook_handler(uwsgi_handler, NULL, NULL, APR_HOOK_MIDDLE);
|
||||
ap_hook_translate_name(uwsgi_translate, NULL, NULL, APR_HOOK_LAST);
|
||||
ap_hook_map_to_storage(uwsgi_map_location, NULL, NULL, APR_HOOK_FIRST);
|
||||
}
|
||||
|
||||
/* Dispatch list for API hooks */
|
||||
module AP_MODULE_DECLARE_DATA uwsgi_module = {
|
||||
STANDARD20_MODULE_STUFF,
|
||||
uwsgi_create_dir_config, /* create per-dir config structs */
|
||||
uwsgi_merge_dir_config, /* merge per-dir config structs */
|
||||
uwsgi_create_server_config, /* create per-server config structs */
|
||||
uwsgi_merge_server_config, /* merge per-server config structs */
|
||||
uwsgi_cmds, /* table of config file commands */
|
||||
uwsgi_register_hooks, /* register hooks */
|
||||
};
|
||||
|
||||
+148
-35
@@ -2,7 +2,7 @@
|
||||
|
||||
*** uWSGI/mod_uwsgi ***
|
||||
|
||||
Copyright 2009 Unbit S.a.s. <info@unbit.it>
|
||||
Copyright 2009-2010 Unbit S.a.s. <info@unbit.it>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
@@ -24,8 +24,8 @@ To compile:
|
||||
apxs2 -i -c mod_uwsgi.c
|
||||
(OSX)
|
||||
sudo apxs -i -a -c mod_uwsgi.c
|
||||
(OSX 64bit)
|
||||
sudo apxs -i -a -c -Wc,'-arch x86_64' -Wl,'-arch x86_64' mod_uwsgi.c
|
||||
(OSX Universal binary)
|
||||
sudo apxs -i -a -c -Wc,"-arch ppc -arch i386 -arch x86_64" -Wl,"-arch ppc -arch i386 -arch x86_64" mod_uwsgi.c
|
||||
|
||||
|
||||
Configure:
|
||||
@@ -71,25 +71,73 @@ typedef struct {
|
||||
uint8_t modifier1;
|
||||
uint8_t modifier2;
|
||||
char script_name[256];
|
||||
char scheme[9];
|
||||
int cgi_mode ;
|
||||
} uwsgi_cfg;
|
||||
|
||||
module AP_MODULE_DECLARE_DATA uwsgi_module;
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
static uint16_t uwsgi_swap16(uint16_t x) {
|
||||
return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int uwsgi_add_var(struct iovec *vec, int i, char *key, char *value, uint16_t *pkt_size) {
|
||||
|
||||
vec[i].iov_base = &vec[i+1].iov_len ;
|
||||
vec[i].iov_len = 2 ;
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
char *ptr;
|
||||
vec[i+2].iov_base = key ;
|
||||
vec[i+2].iov_len = strlen(key) ;
|
||||
#else
|
||||
vec[i+1].iov_base = key ;
|
||||
vec[i+1].iov_len = strlen(key) ;
|
||||
vec[i+2].iov_base = &vec[i+3].iov_len ;
|
||||
vec[i+2].iov_len = 2 ;
|
||||
vec[i+3].iov_base = value ;
|
||||
vec[i+3].iov_len = strlen(value) ;
|
||||
#endif
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
ptr = (char *) &vec[i+2].iov_len;
|
||||
vec[i].iov_base = ptr+sizeof(long)-1 ;
|
||||
vec[i].iov_len = 1 ;
|
||||
vec[i+1].iov_base = ptr+sizeof(long)-2 ;
|
||||
vec[i+1].iov_len = 1 ;
|
||||
#else
|
||||
vec[i].iov_base = &vec[i+1].iov_len ;
|
||||
vec[i].iov_len = 2 ;
|
||||
#endif
|
||||
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
vec[i+5].iov_base = value ;
|
||||
vec[i+5].iov_len = strlen(value) ;
|
||||
#else
|
||||
vec[i+3].iov_base = value ;
|
||||
vec[i+3].iov_len = strlen(value) ;
|
||||
#endif
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
ptr = (char *) &vec[i+5].iov_len;
|
||||
vec[i+3].iov_base = ptr+sizeof(long)-1 ;
|
||||
vec[i+3].iov_len = 1 ;
|
||||
vec[i+4].iov_base = ptr+sizeof(long)-2 ;
|
||||
vec[i+4].iov_len = 1 ;
|
||||
#else
|
||||
vec[i+2].iov_base = &vec[i+3].iov_len ;
|
||||
vec[i+2].iov_len = 2 ;
|
||||
#endif
|
||||
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
*pkt_size+= vec[i+2].iov_len + vec[i+5].iov_len + 4 ;
|
||||
#else
|
||||
*pkt_size+= vec[i+1].iov_len + vec[i+3].iov_len + 4 ;
|
||||
#endif
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
return i+6;
|
||||
#else
|
||||
return i+4;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *uwsgi_server_config(apr_pool_t *p, server_rec *s) {
|
||||
@@ -195,12 +243,17 @@ static int uwsgi_handler(request_rec *r) {
|
||||
|
||||
uwsgi_cfg *c = ap_get_module_config(r->per_dir_config, &uwsgi_module);
|
||||
|
||||
#if APR_IS_BIGENDIAN
|
||||
struct iovec uwsgi_vars[(MAX_VARS*6)+1] ;
|
||||
#else
|
||||
struct iovec uwsgi_vars[(MAX_VARS*4)+1] ;
|
||||
#endif
|
||||
int vecptr = 1 ;
|
||||
char pkt_header[4];
|
||||
uint16_t pkt_size = 0;
|
||||
char buf[4096] ;
|
||||
int cnt,i ;
|
||||
int i ;
|
||||
ssize_t cnt ;
|
||||
const apr_array_header_t *headers;
|
||||
apr_table_entry_t *h;
|
||||
char *penv, *cp;
|
||||
@@ -272,9 +325,16 @@ static int uwsgi_handler(request_rec *r) {
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "SERVER_PROTOCOL", r->protocol, &pkt_size) ;
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REQUEST_URI", r->uri, &pkt_size) ;
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REMOTE_ADDR", r->connection->remote_ip, &pkt_size) ;
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REMOTE_USER", r->user ? r->args : "", &pkt_size) ;
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "REMOTE_USER", r->user ? r->user : "", &pkt_size) ;
|
||||
if (r->user) {
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "AUTH_TYPE", (char *) ap_auth_type(r), &pkt_size) ;
|
||||
}
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "DOCUMENT_ROOT", (char *) ap_document_root(r), &pkt_size) ;
|
||||
|
||||
if (c->scheme[0] != 0) {
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "UWSGI_SCHEME", c->scheme, &pkt_size) ;
|
||||
}
|
||||
|
||||
if (c->script_name[0] == '/') {
|
||||
if (c->script_name[1] == 0) {
|
||||
vecptr = uwsgi_add_var(uwsgi_vars, vecptr, "SCRIPT_NAME", "", &pkt_size) ;
|
||||
@@ -302,6 +362,7 @@ static int uwsgi_handler(request_rec *r) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
headers = apr_table_elts(r->headers_in);
|
||||
h = (apr_table_entry_t *) headers->elts;
|
||||
|
||||
@@ -339,7 +400,13 @@ static int uwsgi_handler(request_rec *r) {
|
||||
uwsgi_vars[0].iov_len = 4;
|
||||
|
||||
pkt_header[0] = c->modifier1 ;
|
||||
#if APR_IS_BIGENDIAN
|
||||
pkt_size = uwsgi_swap16(pkt_size);
|
||||
memcpy(pkt_header+1, &pkt_size, 2);
|
||||
pkt_size = uwsgi_swap16(pkt_size);
|
||||
#else
|
||||
memcpy(pkt_header+1, &pkt_size, 2);
|
||||
#endif
|
||||
pkt_header[3] = c->modifier2 ;
|
||||
|
||||
cnt = writev( uwsgi_poll.fd, uwsgi_vars, vecptr );
|
||||
@@ -354,6 +421,7 @@ static int uwsgi_handler(request_rec *r) {
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (ap_should_client_block(r)) {
|
||||
while ((cnt = ap_get_client_block(r, buf, 4096)) > 0) {
|
||||
@@ -366,62 +434,91 @@ static int uwsgi_handler(request_rec *r) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!c->cgi_mode) {
|
||||
r->assbackwards = 1 ;
|
||||
uwsgi_http_status[13] = 0 ;
|
||||
uwsgi_http_status[12] = 0 ;
|
||||
}
|
||||
|
||||
|
||||
bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
|
||||
|
||||
uwsgi_poll.events = POLLIN ;
|
||||
|
||||
|
||||
|
||||
for(;;) {
|
||||
/* put -1 to disable timeout on zero */
|
||||
cnt = poll(&uwsgi_poll, 1, (c->socket_timeout*1000)-1) ;
|
||||
if (cnt == 0) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: recv() timeout");
|
||||
break;
|
||||
apr_brigade_destroy(bb);
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
else if (cnt > 0) {
|
||||
cnt = recv(uwsgi_poll.fd, buf, 4096, 0) ;
|
||||
if (cnt > 0) {
|
||||
if (!c->cgi_mode && uwsgi_http_status_read < 12) {
|
||||
if (uwsgi_http_status_read + cnt >= 12) {
|
||||
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, 12-uwsgi_http_status_read);
|
||||
r->status = atoi(uwsgi_http_status+8);
|
||||
uwsgi_http_status_read+=cnt;
|
||||
}
|
||||
else {
|
||||
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, cnt);
|
||||
uwsgi_http_status_read+=cnt;
|
||||
}
|
||||
}
|
||||
apr_brigade_write(bb, NULL, NULL, buf, cnt);
|
||||
if (uwsgi_poll.revents & POLLIN) {
|
||||
cnt = recv(uwsgi_poll.fd, buf, 4096, 0) ;
|
||||
if (cnt < 0) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: recv() %s", strerror(errno));
|
||||
apr_brigade_destroy(bb);
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
else if (cnt > 0) {
|
||||
if (!c->cgi_mode && uwsgi_http_status_read < 12) {
|
||||
if (uwsgi_http_status_read + cnt >= 12) {
|
||||
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, 12-uwsgi_http_status_read);
|
||||
r->status = atoi(uwsgi_http_status+8);
|
||||
uwsgi_http_status_read+=cnt;
|
||||
}
|
||||
else {
|
||||
memcpy(uwsgi_http_status+uwsgi_http_status_read, buf, cnt);
|
||||
uwsgi_http_status_read+=cnt;
|
||||
}
|
||||
}
|
||||
// check for client disconnect
|
||||
if (r->connection->aborted) {
|
||||
close(uwsgi_poll.fd);
|
||||
apr_brigade_destroy(bb);
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
apr_brigade_write(bb, NULL, NULL, buf, cnt);
|
||||
hret = ap_fflush(r->output_filters, bb) ;
|
||||
if (hret != APR_SUCCESS) {
|
||||
close(uwsgi_poll.fd);
|
||||
apr_brigade_destroy(bb);
|
||||
return hret;
|
||||
}
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (cnt == 0) {
|
||||
|
||||
if (uwsgi_poll.revents & POLLHUP || uwsgi_poll.revents & POLLERR || uwsgi_poll.revents & POLLNVAL) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: recv() %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
else {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "uwsgi: poll() %s", strerror(errno));
|
||||
break;
|
||||
close(uwsgi_poll.fd);
|
||||
apr_brigade_destroy(bb);
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
close(uwsgi_poll.fd);
|
||||
|
||||
|
||||
b = apr_bucket_eos_create(r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
|
||||
if (!c->cgi_mode) {
|
||||
if (uwsgi_http_status_read == 0) {
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
b = apr_bucket_eos_create(r->connection->bucket_alloc);
|
||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||
|
||||
if (hret = ap_scan_script_header_err_brigade(r, bb, NULL)) {
|
||||
apr_brigade_destroy(bb);
|
||||
@@ -429,7 +526,8 @@ static int uwsgi_handler(request_rec *r) {
|
||||
}
|
||||
}
|
||||
|
||||
return ap_pass_brigade(r->output_filters, bb);
|
||||
|
||||
return ap_pass_brigade(r->output_filters, bb) ;
|
||||
}
|
||||
|
||||
static const char * cmd_uwsgi_force_script_name(cmd_parms *cmd, void *cfg, const char *location) {
|
||||
@@ -446,6 +544,20 @@ static const char * cmd_uwsgi_force_script_name(cmd_parms *cmd, void *cfg, const
|
||||
|
||||
}
|
||||
|
||||
static const char * cmd_uwsgi_force_wsgi_scheme(cmd_parms *cmd, void *cfg, const char *scheme) {
|
||||
uwsgi_cfg *c = cfg;
|
||||
|
||||
if (strlen(scheme) < 9 & strlen(scheme) > 0) {
|
||||
strcpy(c->scheme, scheme);
|
||||
}
|
||||
else {
|
||||
return "ignored uWSGIforceWSGIscheme. Invalid size (max 8 chars)" ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
|
||||
}
|
||||
|
||||
static const char * cmd_uwsgi_modifier1(cmd_parms *cmd, void *cfg, const char *value) {
|
||||
|
||||
uwsgi_cfg *c ;
|
||||
@@ -588,6 +700,7 @@ static const command_rec uwsgi_cmds[] = {
|
||||
AP_INIT_TAKE1("uWSGImodifier2", cmd_uwsgi_modifier2, NULL, RSRC_CONF|ACCESS_CONF, "Set uWSGI modifier2"),
|
||||
AP_INIT_TAKE1("uWSGIforceScriptName", cmd_uwsgi_force_script_name, NULL, ACCESS_CONF, "Fix for PATH_INFO/SCRIPT_NAME when the location has filesystem correspondence"),
|
||||
AP_INIT_TAKE1("uWSGIforceCGImode", cmd_uwsgi_force_cgi_mode, NULL, ACCESS_CONF, "Force uWSGI CGI mode for perfect integration with apache filter"),
|
||||
AP_INIT_TAKE1("uWSGIforceWSGIscheme", cmd_uwsgi_force_wsgi_scheme, NULL, ACCESS_CONF, "Force the WSGI scheme var (set by default to \"http\")"),
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
#ifdef UWSGI_ASYNC
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <sys/epoll.h>
|
||||
|
||||
int async_queue_init(int serverfd) {
|
||||
int epfd ;
|
||||
struct epoll_event ee;
|
||||
|
||||
epfd = epoll_create(256);
|
||||
|
||||
if (epfd < 0) {
|
||||
uwsgi_error("epoll_create()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
memset(&ee, 0, sizeof(struct epoll_event));
|
||||
ee.events = EPOLLIN;
|
||||
ee.data.fd = serverfd;
|
||||
|
||||
if (epoll_ctl(epfd, EPOLL_CTL_ADD, serverfd, &ee)) {
|
||||
uwsgi_error("epoll_ctl()");
|
||||
close(epfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return epfd;
|
||||
}
|
||||
|
||||
int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
|
||||
int ret ;
|
||||
|
||||
if (timeout <= 0) {
|
||||
timeout = block;
|
||||
}
|
||||
else {
|
||||
timeout = timeout*1000;
|
||||
}
|
||||
|
||||
//uwsgi_log("waiting with timeout %d nevents %d\n", timeout, nevents);
|
||||
ret = epoll_wait(queuefd, (struct epoll_event *) events, nevents, timeout);
|
||||
if (ret < 0) {
|
||||
uwsgi_error("epoll_wait()");
|
||||
}
|
||||
return ret ;
|
||||
}
|
||||
|
||||
int async_add(int queuefd, int fd, int etype) {
|
||||
struct epoll_event ee;
|
||||
|
||||
memset(&ee, 0, sizeof(struct epoll_event));
|
||||
ee.events = etype;
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_ADD, fd, &ee)) {
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int async_mod(int queuefd, int fd, int etype) {
|
||||
struct epoll_event ee;
|
||||
|
||||
memset(&ee, 0, sizeof(struct epoll_event));
|
||||
ee.events = etype;
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_MOD, fd, &ee)) {
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int async_del(int queuefd, int fd, int etype) {
|
||||
struct epoll_event ee;
|
||||
|
||||
memset(&ee, 0, sizeof(struct epoll_event));
|
||||
ee.events = etype;
|
||||
ee.data.fd = fd;
|
||||
|
||||
if (epoll_ctl(queuefd, EPOLL_CTL_DEL, fd, &ee)) {
|
||||
uwsgi_error("epoll_ctl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#elif defined(__sun__)
|
||||
|
||||
int async_queue_init(int serverfd) {
|
||||
int dpfd ;
|
||||
struct pollfd dpev;
|
||||
|
||||
|
||||
dpfd = open("/dev/poll", O_RDWR);
|
||||
|
||||
if (dpfd < 0) {
|
||||
uwsgi_error("open()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
|
||||
dpev.fd = serverfd;
|
||||
dpev.events = POLLIN ;
|
||||
dpev.revents = 0;
|
||||
|
||||
if (write(dpfd, &dpev, sizeof(struct pollfd)) < 0) {
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
return dpfd;
|
||||
}
|
||||
|
||||
int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
|
||||
int ret ;
|
||||
struct dvpoll dv ;
|
||||
|
||||
if (timeout <= 0) {
|
||||
timeout = block;
|
||||
}
|
||||
else {
|
||||
timeout = timeout*1000;
|
||||
}
|
||||
|
||||
dv.dp_fds = (struct pollfd *) events;
|
||||
dv.dp_nfds = nevents;
|
||||
dv.dp_timeout = timeout;
|
||||
|
||||
//uwsgi_log("waiting with timeout %d nevents %d\n", timeout, nevents);
|
||||
ret = ioctl(queuefd, DP_POLL, &dv);
|
||||
if (ret < 0) {
|
||||
uwsgi_error("ioctl()");
|
||||
}
|
||||
return ret ;
|
||||
}
|
||||
|
||||
int async_add(int queuefd, int fd, int etype) {
|
||||
struct pollfd pl;
|
||||
|
||||
pl.fd = fd ;
|
||||
pl.events = etype ;
|
||||
pl.revents = 0 ;
|
||||
|
||||
if (write(queuefd, &pl, sizeof(struct pollfd)) < 0) {
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int async_mod(int queuefd, int fd, int etype) {
|
||||
// using the same fd will overwrite existing rule
|
||||
return async_add(queuefd, fd, etype);
|
||||
}
|
||||
|
||||
int async_del(int queuefd, int fd, int etype) {
|
||||
// use POLLREMOVE to remove an fd
|
||||
return async_add(queuefd, fd, POLLREMOVE);
|
||||
}
|
||||
|
||||
#else
|
||||
int async_queue_init(int serverfd) {
|
||||
int kfd ;
|
||||
struct kevent kev;
|
||||
|
||||
kfd = kqueue();
|
||||
|
||||
if (kfd < 0) {
|
||||
uwsgi_error("kqueue()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
EV_SET(&kev, serverfd, EVFILT_READ, EV_ADD, 0, 0, 0);
|
||||
if (kevent(kfd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return kfd;
|
||||
}
|
||||
|
||||
int async_wait(int queuefd, void *events, int nevents, int block, int timeout) {
|
||||
|
||||
int ret;
|
||||
struct timespec ts ;
|
||||
|
||||
|
||||
if (timeout <= 0) {
|
||||
if (!block) {
|
||||
memset(&ts, 0, sizeof(struct timespec));
|
||||
ts.tv_sec = timeout ;
|
||||
ret = kevent(queuefd, NULL, 0, events, nevents, &ts);
|
||||
}
|
||||
else {
|
||||
ret = kevent(queuefd, NULL, 0, events, nevents, NULL);
|
||||
}
|
||||
}
|
||||
else {
|
||||
memset(&ts, 0, sizeof(struct timespec));
|
||||
ts.tv_sec = timeout ;
|
||||
ret = kevent(queuefd, NULL, 0, events, nevents, &ts);
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int async_add(int queuefd, int fd, int etype) {
|
||||
struct kevent kev;
|
||||
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_ADD, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int async_mod(int queuefd, int fd, int etype) {
|
||||
struct kevent kev;
|
||||
|
||||
EV_SET(&kev, fd, ASYNC_OUT, EV_DISABLE, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_ADD, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int async_del(int queuefd, int fd, int etype) {
|
||||
struct kevent kev;
|
||||
|
||||
EV_SET(&kev, fd, etype, EV_DELETE, 0, 0, 0);
|
||||
if (kevent(queuefd, &kev, 1, NULL, 0, NULL) < 0) {
|
||||
uwsgi_error("kevent()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline struct wsgi_request *next_wsgi_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
uint8_t *ptr = (uint8_t *) wsgi_req ;
|
||||
|
||||
ptr += sizeof(struct wsgi_request) ;
|
||||
|
||||
return (struct wsgi_request *) ptr ;
|
||||
}
|
||||
|
||||
int async_get_timeout(struct uwsgi_server *uwsgi) {
|
||||
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
time_t curtime, tdelta = 0 ;
|
||||
int ret = 0 ;
|
||||
|
||||
if (!uwsgi->async_running) return 0;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_AGAIN) {
|
||||
if (wsgi_req->async_timeout_expired) {
|
||||
return 0;
|
||||
}
|
||||
if (wsgi_req->async_timeout > 0) {
|
||||
if (tdelta <= 0 || tdelta > wsgi_req->async_timeout) {
|
||||
tdelta = wsgi_req->async_timeout ;
|
||||
}
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
curtime = time(NULL);
|
||||
|
||||
ret = tdelta - curtime ;
|
||||
if (ret > 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void async_expire_timeouts(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
time_t deadline = time(NULL);
|
||||
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_AGAIN && wsgi_req->async_timeout > 0) {
|
||||
if (wsgi_req->async_timeout <= deadline) {
|
||||
wsgi_req->async_timeout = 0 ;
|
||||
wsgi_req->async_timeout_expired = 1 ;
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
|
||||
struct wsgi_request *find_first_available_wsgi_req(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_OK) {
|
||||
return wsgi_req ;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
struct wsgi_request *find_wsgi_req_by_id(struct uwsgi_server *uwsgi, int async_id) {
|
||||
|
||||
uint8_t *ptr = (uint8_t *) uwsgi->wsgi_requests ;
|
||||
|
||||
ptr += sizeof(struct wsgi_request) * async_id ;
|
||||
|
||||
return (struct wsgi_request *) ptr ;
|
||||
}
|
||||
|
||||
struct wsgi_request *find_wsgi_req_by_fd(struct uwsgi_server *uwsgi, int fd, int etype) {
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
|
||||
if (etype != -1) {
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_waiting_fd == fd && wsgi_req->async_waiting_fd_type == etype) {
|
||||
return wsgi_req ;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_waiting_fd == fd) {
|
||||
return wsgi_req ;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
|
||||
}
|
||||
|
||||
void async_set_timeout(struct wsgi_request *wsgi_req, time_t timeout) {
|
||||
|
||||
wsgi_req->async_timeout = time(NULL);
|
||||
wsgi_req->async_timeout += timeout;
|
||||
wsgi_req->async_timeout_expired = 0 ;
|
||||
|
||||
}
|
||||
|
||||
void async_write_all(struct uwsgi_server *uwsgi, char *data, size_t len) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i;
|
||||
ssize_t rlen ;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
rlen = write(wsgi_req->poll.fd, data, len);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
else {
|
||||
wsgi_req->response_size += rlen ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void async_unpause_all(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
wsgi_req->async_status = UWSGI_AGAIN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct wsgi_request * async_loop(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request *wsgi_req ;
|
||||
int i ;
|
||||
|
||||
uwsgi->async_running = -1 ;
|
||||
wsgi_req = uwsgi->wsgi_requests ;
|
||||
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_AGAIN) {
|
||||
if (wsgi_req->async_waiting_fd != -1 && !wsgi_req->async_waiting_fd_monitored) {
|
||||
// add fd to monitoring
|
||||
if (async_add(uwsgi->async_queue, wsgi_req->async_waiting_fd, wsgi_req->async_waiting_fd_type)) {
|
||||
// error adding fd to the async queue, better to close it...
|
||||
close(wsgi_req->async_waiting_fd);
|
||||
wsgi_req->async_status = UWSGI_OK ;
|
||||
return wsgi_req;
|
||||
}
|
||||
wsgi_req->async_waiting_fd_monitored = 1;
|
||||
wsgi_req->async_status = UWSGI_AGAIN;
|
||||
}
|
||||
else if (wsgi_req->async_waiting_fd == -1 && wsgi_req->async_timeout <= 0) {
|
||||
uwsgi->async_running = 0 ;
|
||||
// st global wsgi_req for python functions
|
||||
uwsgi->wsgi_req = wsgi_req ;
|
||||
wsgi_req->async_status = (*uwsgi->shared->hooks[wsgi_req->uh.modifier1]) (uwsgi, wsgi_req);
|
||||
|
||||
wsgi_req->async_switches++;
|
||||
|
||||
if (wsgi_req->async_status < UWSGI_AGAIN) {
|
||||
return wsgi_req;
|
||||
}
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -1,25 +0,0 @@
|
||||
all: clean uwsgi
|
||||
|
||||
uwsgi: utils.o protocol.o socket.o pymodule.o spooler.o main.o
|
||||
$(CC) $(LD_FLAGS) utils.o protocol.o socket.o spooler.o pymodule.o main.o -o $(PROGRAM)
|
||||
|
||||
utils.o: utils.c
|
||||
$(CC) -c $(CFLAGS) utils.c
|
||||
|
||||
protocol.o: protocol.c
|
||||
$(CC) -c $(CFLAGS) protocol.c
|
||||
|
||||
socket.o: socket.c
|
||||
$(CC) -c $(CFLAGS) socket.c
|
||||
|
||||
spooler.o: spooler.c
|
||||
$(CC) -c $(CFLAGS) spooler.c
|
||||
|
||||
pymodule.o: uwsgi_pymodule.c
|
||||
$(CC) -c $(CFLAGS) -o pymodule.o uwsgi_pymodule.c
|
||||
|
||||
main.o: uwsgi.c
|
||||
$(CC) -c $(CFLAGS) -o main.o uwsgi.c
|
||||
|
||||
clean:
|
||||
rm -f utils.o protocol.o socket.o pymodule.o spooler.o main.o
|
||||
@@ -0,0 +1 @@
|
||||
the uwsgi handler is included in the official Cherokee distribution
|
||||
@@ -1,92 +0,0 @@
|
||||
from Form import *
|
||||
from Table import *
|
||||
from Module import *
|
||||
from validations import *
|
||||
from consts import *
|
||||
|
||||
# For gettext
|
||||
N_ = lambda x: x
|
||||
|
||||
from ModuleCgi import *
|
||||
from ModuleBalancer import NOTE_BALANCER
|
||||
|
||||
HELPS = [
|
||||
('modules_handlers_uwsgi', "UWSGI")
|
||||
]
|
||||
|
||||
class ModuleUwsgi (ModuleCgiBase):
|
||||
PROPERTIES = ModuleCgiBase.PROPERTIES + [
|
||||
'balancer',
|
||||
'modifier1',
|
||||
'modifier2',
|
||||
'pass_wsgi_vars',
|
||||
'pass_request_body'
|
||||
]
|
||||
|
||||
def __init__ (self, cfg, prefix, submit):
|
||||
ModuleCgiBase.__init__ (self, cfg, prefix, 'uwsgi', submit)
|
||||
|
||||
self.show_script_alias = False
|
||||
self.show_change_uid = False
|
||||
self.show_document_root = True
|
||||
|
||||
def _op_render (self):
|
||||
txt = ModuleCgiBase._op_render (self)
|
||||
|
||||
txt += '<h2>%s</h2>' % (_('UWSGI specific'))
|
||||
|
||||
table = TableProps()
|
||||
|
||||
|
||||
opt = "%s!modifier1" % (self._prefix)
|
||||
self.AddPropEntry(table, _('Modifier1'), opt, 'a number between 0 and 255', size=3);
|
||||
|
||||
opt = "%s!modifier2" % (self._prefix)
|
||||
self.AddPropEntry(table, _('Modifier2'), opt, 'a number between 0 and 255', size=3);
|
||||
|
||||
opt = "%s!pass_wsgi_vars" % (self._prefix)
|
||||
self.AddPropCheck (table, _("Pass WSGI vars"), opt, True, 'pass all the wsgi vars to the application')
|
||||
|
||||
opt = "%s!pass_request_body" % (self._prefix)
|
||||
self.AddPropCheck (table, _("Pass Request body"), opt, True, 'pass the request body to the application')
|
||||
|
||||
opt = "%s!balancer" % (self._prefix)
|
||||
e = self.AddPropOptions_Reload_Module (table, _("Balancer"), opt,
|
||||
modules_available(BALANCERS), _(NOTE_BALANCER))
|
||||
|
||||
txt += self.Indent(str(table) + e)
|
||||
|
||||
return txt
|
||||
|
||||
def _op_apply_changes (self, uri, post):
|
||||
|
||||
# Apply modifier1
|
||||
opt = "%s!modifier1" % (self._prefix)
|
||||
mod1 = post.pop(opt)
|
||||
if mod1:
|
||||
self._cfg[opt] = mod1
|
||||
|
||||
# Apply modifier2
|
||||
opt = "%s!modifier2" % (self._prefix)
|
||||
mod2 = post.pop(opt)
|
||||
if mod2:
|
||||
self._cfg[opt] = mod2
|
||||
|
||||
# Apply pass_request_body and pass_wsgi_vars
|
||||
self.ApplyChangesPrefix (self._prefix, ['pass_wsgi_vars','pass_request_body'], post)
|
||||
|
||||
# Apply balancer changes
|
||||
pre = "%s!balancer" % (self._prefix)
|
||||
|
||||
new_balancer = post.pop(pre)
|
||||
if new_balancer:
|
||||
self._cfg[pre] = new_balancer
|
||||
|
||||
cfg = self._cfg[pre]
|
||||
if cfg and cfg.value:
|
||||
name = cfg.value
|
||||
props = module_obj_factory (name, self._cfg, pre, self.submit_url)
|
||||
props._op_apply_changes (uri, post)
|
||||
|
||||
# And CGI changes
|
||||
return ModuleCgiBase._op_apply_changes (self, uri, post)
|
||||
@@ -1,452 +0,0 @@
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
||||
|
||||
/* Cherokee
|
||||
*
|
||||
* Authors:
|
||||
* Alvaro Lopez Ortega <alvaro@alobbs.com>
|
||||
*
|
||||
* Copyright (C) 2001-2009 Alvaro Lopez Ortega
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "common-internal.h"
|
||||
#include "handler_uwsgi.h"
|
||||
|
||||
#include "connection.h"
|
||||
#include "source_interpreter.h"
|
||||
#include "thread.h"
|
||||
#include "util.h"
|
||||
#include "connection-protected.h"
|
||||
#include "bogotime.h"
|
||||
|
||||
#define ENTRIES "handler,cgi"
|
||||
|
||||
/* Plug-in initialization
|
||||
*/
|
||||
CGI_LIB_INIT (uwsgi, http_all_methods);
|
||||
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
static uint16_t uwsgi_swap16(uint16_t x) {
|
||||
return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Methods implementation
|
||||
*/
|
||||
static ret_t
|
||||
props_free (cherokee_handler_uwsgi_props_t *props)
|
||||
{
|
||||
if (props->balancer)
|
||||
cherokee_balancer_free (props->balancer);
|
||||
|
||||
return cherokee_handler_cgi_base_props_free (PROP_CGI_BASE(props));
|
||||
}
|
||||
|
||||
ret_t
|
||||
cherokee_handler_uwsgi_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
|
||||
{
|
||||
ret_t ret;
|
||||
cherokee_list_t *i;
|
||||
cherokee_handler_uwsgi_props_t *props;
|
||||
|
||||
/* Instance a new property object
|
||||
*/
|
||||
if (*_props == NULL) {
|
||||
CHEROKEE_NEW_STRUCT (n, handler_uwsgi_props);
|
||||
|
||||
cherokee_handler_cgi_base_props_init_base (PROP_CGI_BASE(n),
|
||||
MODULE_PROPS_FREE(props_free));
|
||||
|
||||
n->balancer = NULL;
|
||||
n->modifier1 = 0 ;
|
||||
n->modifier2 = 0 ;
|
||||
n->pass_wsgi_vars = true;
|
||||
n->pass_request_body = true;
|
||||
|
||||
*_props = MODULE_PROPS(n);
|
||||
}
|
||||
|
||||
props = PROP_UWSGI(*_props);
|
||||
|
||||
/* Parse the configuration tree
|
||||
*/
|
||||
cherokee_config_node_foreach (i, conf) {
|
||||
cherokee_config_node_t *subconf = CONFIG_NODE(i);
|
||||
|
||||
if (equal_buf_str (&subconf->key, "balancer")) {
|
||||
ret = cherokee_balancer_instance (&subconf->val, subconf, srv, &props->balancer);
|
||||
if (ret != ret_ok) return ret;
|
||||
}
|
||||
else if (equal_buf_str (&subconf->key, "modifier1")) {
|
||||
props->modifier1 = (uint8_t) atoi(subconf->val.buf);
|
||||
}
|
||||
else if (equal_buf_str (&subconf->key, "modifier2")) {
|
||||
props->modifier2 = (uint8_t) atoi(subconf->val.buf);
|
||||
}
|
||||
else if (equal_buf_str (&subconf->key, "pass_wsgi_vars")) {
|
||||
props->pass_wsgi_vars = !! atoi (subconf->val.buf);
|
||||
}
|
||||
else if (equal_buf_str (&subconf->key, "pass_request_body")) {
|
||||
props->pass_request_body = !! atoi (subconf->val.buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* Init base class
|
||||
*/
|
||||
ret = cherokee_handler_cgi_base_configure (conf, srv, _props);
|
||||
if (ret != ret_ok) return ret;
|
||||
|
||||
/* Final checks
|
||||
*/
|
||||
if (props->balancer == NULL) {
|
||||
LOG_CRITICAL_S (CHEROKEE_ERROR_HANDLER_NO_BALANCER);
|
||||
return ret_error;
|
||||
}
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
add_env_pair (cherokee_handler_cgi_base_t *cgi_base,
|
||||
const char *key, int key_len,
|
||||
const char *val, int val_len)
|
||||
{
|
||||
cherokee_handler_uwsgi_t *uwsgi = HDL_UWSGI(cgi_base);
|
||||
uint16_t u_key_len = (uint16_t) key_len ;
|
||||
uint16_t u_val_len = (uint16_t) val_len ;
|
||||
|
||||
|
||||
/* 2 bytes for every string (16 bit le) */
|
||||
cherokee_buffer_ensure_size (&uwsgi->header, uwsgi->header.len + key_len + val_len + 4);
|
||||
|
||||
/* force to le if cherokee is big-endian */
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_key_len = uwsgi_swap16(u_key_len);
|
||||
#endif
|
||||
cherokee_buffer_add (&uwsgi->header, (const char *) &u_key_len, 2);
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_key_len = uwsgi_swap16(u_key_len);
|
||||
#endif
|
||||
cherokee_buffer_add (&uwsgi->header, key, key_len);
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_val_len = uwsgi_swap16(u_val_len);
|
||||
#endif
|
||||
cherokee_buffer_add (&uwsgi->header, (const char *) &u_val_len, 2);
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_val_len = uwsgi_swap16(u_val_len);
|
||||
#endif
|
||||
cherokee_buffer_add (&uwsgi->header, val, val_len);
|
||||
}
|
||||
|
||||
|
||||
static ret_t
|
||||
read_from_uwsgi (cherokee_handler_cgi_base_t *cgi_base, cherokee_buffer_t *buffer)
|
||||
{
|
||||
ret_t ret;
|
||||
size_t read = 0;
|
||||
cherokee_handler_uwsgi_t *uwsgi = HDL_UWSGI(cgi_base);
|
||||
|
||||
ret = cherokee_socket_bufread (&uwsgi->socket, buffer, 4096, &read);
|
||||
|
||||
switch (ret) {
|
||||
case ret_eagain:
|
||||
cherokee_thread_deactive_to_polling (HANDLER_THREAD(cgi_base), HANDLER_CONN(cgi_base),
|
||||
uwsgi->socket.socket, 0, false);
|
||||
return ret_eagain;
|
||||
|
||||
case ret_ok:
|
||||
TRACE (ENTRIES, "%d bytes read\n", read);
|
||||
return ret_ok;
|
||||
|
||||
case ret_eof:
|
||||
case ret_error:
|
||||
cgi_base->got_eof = true;
|
||||
return ret;
|
||||
|
||||
default:
|
||||
RET_UNKNOWN(ret);
|
||||
}
|
||||
|
||||
SHOULDNT_HAPPEN;
|
||||
return ret_error;
|
||||
}
|
||||
|
||||
|
||||
ret_t
|
||||
cherokee_handler_uwsgi_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props)
|
||||
{
|
||||
CHEROKEE_NEW_STRUCT (n, handler_uwsgi);
|
||||
|
||||
/* Init the base class
|
||||
*/
|
||||
cherokee_handler_cgi_base_init (
|
||||
HDL_CGI_BASE(n), cnt,
|
||||
PLUGIN_INFO_HANDLER_PTR(uwsgi),
|
||||
HANDLER_PROPS(props),
|
||||
add_env_pair, read_from_uwsgi);
|
||||
|
||||
/* Virtual methods
|
||||
*/
|
||||
MODULE(n)->init = (handler_func_init_t) cherokee_handler_uwsgi_init;
|
||||
MODULE(n)->free = (module_func_free_t) cherokee_handler_uwsgi_free;
|
||||
|
||||
/* Virtual methods: implemented by handler_cgi_base
|
||||
*/
|
||||
HANDLER(n)->step = (handler_func_step_t) cherokee_handler_cgi_base_step;
|
||||
HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_cgi_base_add_headers;
|
||||
|
||||
/* Properties
|
||||
*/
|
||||
n->post_len = 0;
|
||||
n->src_ref = NULL;
|
||||
|
||||
cherokee_buffer_init (&n->header);
|
||||
cherokee_socket_init (&n->socket);
|
||||
|
||||
/* Return the object
|
||||
*/
|
||||
*hdl = HANDLER(n);
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
ret_t
|
||||
cherokee_handler_uwsgi_free (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
/* Free the rest of the handler CGI memory
|
||||
*/
|
||||
cherokee_handler_cgi_base_free (HDL_CGI_BASE(hdl));
|
||||
|
||||
/* UWSGI stuff
|
||||
*/
|
||||
cherokee_socket_close (&hdl->socket);
|
||||
cherokee_socket_mrproper (&hdl->socket);
|
||||
|
||||
cherokee_buffer_mrproper (&hdl->header);
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
static ret_t
|
||||
uwsgi_fix_packet (cherokee_buffer_t *buf, uint8_t modifier1, uint8_t modifier2)
|
||||
{
|
||||
|
||||
uwsgi_header uh;
|
||||
|
||||
uh.modifier1 = modifier1 ;
|
||||
uh.modifier2 = modifier2 ;
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
uh.env_size = uwsgi_swap16((uint16_t) buf->len) ;
|
||||
#else
|
||||
uh.env_size = (uint16_t) buf->len ;
|
||||
#endif
|
||||
|
||||
cherokee_buffer_ensure_size (buf, buf->len + 4);
|
||||
cherokee_buffer_prepend (buf, (const char * )&uh, 4);
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
static ret_t
|
||||
build_header (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
|
||||
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
|
||||
|
||||
cuint_t len;
|
||||
char tmp[64];
|
||||
|
||||
if (props->pass_request_body == true && props->pass_wsgi_vars == true) {
|
||||
len = snprintf (tmp, sizeof(tmp), FMT_OFFSET, (CST_OFFSET)hdl->post_len);
|
||||
add_env_pair(HDL_CGI_BASE(hdl), "CONTENT_LENGTH", 14, tmp, len);
|
||||
}
|
||||
|
||||
if (props->pass_wsgi_vars == true) {
|
||||
cherokee_handler_cgi_base_build_envp (HDL_CGI_BASE(hdl), HANDLER_CONN(hdl));
|
||||
}
|
||||
|
||||
return uwsgi_fix_packet (&hdl->header, props->modifier1, props->modifier2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static ret_t
|
||||
connect_to_server (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
ret_t ret;
|
||||
cherokee_connection_t *conn = HANDLER_CONN(hdl);
|
||||
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
|
||||
|
||||
/* Get a reference to the target host
|
||||
*/
|
||||
if (hdl->src_ref == NULL) {
|
||||
ret = cherokee_balancer_dispatch (props->balancer, conn, &hdl->src_ref);
|
||||
if (ret != ret_ok)
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Try to connect
|
||||
*/
|
||||
if (hdl->src_ref->type == source_host) {
|
||||
ret = cherokee_source_connect_polling (hdl->src_ref, &hdl->socket, conn);
|
||||
if ((ret == ret_deny) || (ret == ret_error))
|
||||
{
|
||||
cherokee_balancer_report_fail (props->balancer, conn, hdl->src_ref);
|
||||
}
|
||||
} else {
|
||||
ret = cherokee_source_interpreter_connect_polling (SOURCE_INT(hdl->src_ref),
|
||||
&hdl->socket, conn);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static ret_t
|
||||
send_header (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
ret_t ret;
|
||||
size_t written = 0;
|
||||
cherokee_connection_t *conn = HANDLER_CONN(hdl);
|
||||
|
||||
ret = cherokee_socket_bufwrite (&hdl->socket, &hdl->header, &written);
|
||||
if (ret != ret_ok) {
|
||||
conn->error_code = http_bad_gateway;
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if 0
|
||||
cherokee_buffer_print_debug (&hdl->header, -1);
|
||||
#endif
|
||||
cherokee_buffer_move_to_begin (&hdl->header, written);
|
||||
|
||||
TRACE (ENTRIES, "sent remaining=%d\n", hdl->header.len);
|
||||
|
||||
if (! cherokee_buffer_is_empty (&hdl->header))
|
||||
return ret_eagain;
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
static ret_t
|
||||
send_post (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
ret_t ret;
|
||||
int e_fd = -1;
|
||||
int mode = 0;
|
||||
cherokee_connection_t *conn = HANDLER_CONN(hdl);
|
||||
|
||||
ret = cherokee_post_walk_to_fd (&conn->post, hdl->socket.socket, &e_fd, &mode);
|
||||
|
||||
switch (ret) {
|
||||
case ret_ok:
|
||||
break;
|
||||
case ret_eagain:
|
||||
if (e_fd != -1)
|
||||
cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl), conn, e_fd, mode, false);
|
||||
return ret_eagain;
|
||||
default:
|
||||
conn->error_code = http_bad_gateway;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
|
||||
|
||||
ret_t
|
||||
cherokee_handler_uwsgi_init (cherokee_handler_uwsgi_t *hdl)
|
||||
{
|
||||
ret_t ret;
|
||||
cherokee_connection_t *conn = HANDLER_CONN(hdl);
|
||||
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
|
||||
|
||||
switch (HDL_CGI_BASE(hdl)->init_phase) {
|
||||
case hcgi_phase_build_headers:
|
||||
TRACE (ENTRIES, "Init: %s\n", "begins");
|
||||
|
||||
/* Extracts PATH_INFO and filename from request uri
|
||||
*/
|
||||
ret = cherokee_handler_cgi_base_extract_path (HDL_CGI_BASE(hdl), false);
|
||||
if (unlikely (ret < ret_ok)) {
|
||||
conn->error_code = http_internal_error;
|
||||
return ret_error;
|
||||
}
|
||||
|
||||
/* Prepare Post
|
||||
*/
|
||||
if (! cherokee_post_is_empty (&conn->post)) {
|
||||
cherokee_post_walk_reset (&conn->post);
|
||||
cherokee_post_get_len (&conn->post, &hdl->post_len);
|
||||
}
|
||||
|
||||
/* Build the headers
|
||||
*/
|
||||
ret = build_header (hdl);
|
||||
if (unlikely (ret != ret_ok)) {
|
||||
conn->error_code = http_internal_error;
|
||||
return ret_error;
|
||||
}
|
||||
|
||||
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_connect;
|
||||
|
||||
case hcgi_phase_connect:
|
||||
TRACE (ENTRIES, "Init: %s\n", "connect");
|
||||
|
||||
/* Connect
|
||||
*/
|
||||
ret = connect_to_server (hdl);
|
||||
switch (ret) {
|
||||
case ret_ok:
|
||||
break;
|
||||
case ret_eagain:
|
||||
return ret_eagain;
|
||||
case ret_deny:
|
||||
conn->error_code = http_gateway_timeout;
|
||||
return ret_error;
|
||||
default:
|
||||
conn->error_code = http_service_unavailable;
|
||||
return ret_error;
|
||||
}
|
||||
|
||||
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_headers;
|
||||
|
||||
case hcgi_phase_send_headers:
|
||||
TRACE (ENTRIES, "Init: %s\n", "send_headers");
|
||||
|
||||
/* Send the header
|
||||
*/
|
||||
ret = send_header (hdl);
|
||||
if (ret != ret_ok)
|
||||
return ret;
|
||||
|
||||
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_post;
|
||||
|
||||
case hcgi_phase_send_post:
|
||||
/* Send the Post
|
||||
*/
|
||||
if (hdl->post_len > 0 && props->pass_request_body == true) {
|
||||
return send_post (hdl);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return ret_ok;
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
||||
|
||||
/* Cherokee
|
||||
*
|
||||
* Authors:
|
||||
* Alvaro Lopez Ortega <alvaro@alobbs.com>
|
||||
*
|
||||
* Copyright (C) 2001-2009 Alvaro Lopez Ortega
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of version 2 of the GNU General Public
|
||||
* License as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||||
* 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef CHEROKEE_HANDLER_UWSGI_H
|
||||
#define CHEROKEE_HANDLER_UWSGI_H
|
||||
|
||||
#include "common-internal.h"
|
||||
|
||||
#include "handler.h"
|
||||
#include "buffer.h"
|
||||
#include "plugin_loader.h"
|
||||
#include "socket.h"
|
||||
#include "handler_cgi_base.h"
|
||||
#include "balancer.h"
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t modifier1;
|
||||
uint16_t env_size;
|
||||
uint8_t modifier2;
|
||||
} uwsgi_header ;
|
||||
|
||||
|
||||
typedef struct {
|
||||
cherokee_handler_cgi_base_t base;
|
||||
cherokee_balancer_t *balancer;
|
||||
uint8_t modifier1;
|
||||
uint8_t modifier2;
|
||||
cherokee_boolean_t pass_wsgi_vars;
|
||||
cherokee_boolean_t pass_request_body;
|
||||
} cherokee_handler_uwsgi_props_t;
|
||||
|
||||
|
||||
typedef struct {
|
||||
cherokee_handler_cgi_base_t base;
|
||||
cherokee_buffer_t header;
|
||||
cherokee_socket_t socket;
|
||||
cherokee_source_t *src_ref;
|
||||
time_t spawned;
|
||||
off_t post_len;
|
||||
} cherokee_handler_uwsgi_t;
|
||||
|
||||
#define HDL_UWSGI(x) ((cherokee_handler_uwsgi_t *)(x))
|
||||
#define PROP_UWSGI(x) ((cherokee_handler_uwsgi_props_t *)(x))
|
||||
#define HANDLER_UWSGI_PROPS(x) (PROP_UWSGI(MODULE(x)->props))
|
||||
|
||||
|
||||
/* Library init function
|
||||
*/
|
||||
void PLUGIN_INIT_NAME(uwsgi) (cherokee_plugin_loader_t *loader);
|
||||
|
||||
/* Methods
|
||||
*/
|
||||
ret_t cherokee_handler_uwsgi_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props);
|
||||
ret_t cherokee_handler_uwsgi_free (cherokee_handler_uwsgi_t *hdl);
|
||||
ret_t cherokee_handler_uwsgi_init (cherokee_handler_uwsgi_t *hdl);
|
||||
|
||||
#endif /* CHEROKEE_HANDLER_UWSGI_H */
|
||||
@@ -1 +0,0 @@
|
||||
file
|
||||
@@ -0,0 +1,144 @@
|
||||
package Plack::Handler::Uwsgi;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use IO::Socket::INET;
|
||||
use HTTP::Status;
|
||||
|
||||
use Plack::Util;
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $self = bless {@_}, $class;
|
||||
$self;
|
||||
}
|
||||
|
||||
sub run {
|
||||
my ($self, $app) = @_;
|
||||
|
||||
my $server ;
|
||||
|
||||
if (exists($ENV{'UWSGI_FD'})) {
|
||||
$server = IO::Socket::UNIX->new_from_fd($ENV{'UWSGI_FD'}, '+<');
|
||||
}
|
||||
else {
|
||||
$server = IO::Socket::INET->new(LocalPort => $self->{port}, LocalAddr => $self->{host}, Listen => 100, ReuseAddr => 1);
|
||||
}
|
||||
|
||||
while ( my $client = $server->accept ) {
|
||||
|
||||
my $head = '';
|
||||
my $remains = 4 ;
|
||||
|
||||
while($remains) {
|
||||
$client->recv(my $buf, $remains);
|
||||
last unless $buf;
|
||||
$head.=$buf;
|
||||
$remains -= length($buf);
|
||||
}
|
||||
|
||||
if (length($head) != 4) {
|
||||
$client->close ;
|
||||
next;
|
||||
}
|
||||
|
||||
my ($mod1, $envsize, $mod2) = unpack('CvC', $head) ;
|
||||
|
||||
unless ($envsize) {
|
||||
$client->close ;
|
||||
next;
|
||||
}
|
||||
|
||||
$remains = $envsize ;
|
||||
my $envbuf = '' ;
|
||||
while($remains) {
|
||||
my $buf ;
|
||||
if ($remains >= 4096) {
|
||||
$client->recv($buf, 4096);
|
||||
}
|
||||
else {
|
||||
$client->recv($buf, $remains);
|
||||
}
|
||||
|
||||
unless($buf) {
|
||||
$client->close ;
|
||||
next;
|
||||
}
|
||||
|
||||
$envbuf.=$buf;
|
||||
$remains -= length($buf);
|
||||
}
|
||||
|
||||
if (length($envbuf) != $envsize ) {
|
||||
$client->close ;
|
||||
next;
|
||||
}
|
||||
|
||||
my %env ;
|
||||
|
||||
my $i = 0;
|
||||
while($i < $envsize) {
|
||||
my $kl = unpack('v', substr($envbuf, $i, 2)) ; $i+=2;
|
||||
my $key = substr($envbuf, $i, $kl); $i+=$kl;
|
||||
my $vl = unpack('v', substr($envbuf, $i, 2)) ; $i+=2;
|
||||
$env{$key} = substr($envbuf, $i, $vl); $i+=$vl;
|
||||
}
|
||||
|
||||
my $env = {
|
||||
%env,
|
||||
'psgi.version' => [1,1],
|
||||
'psgi.url_scheme' => ($env{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
|
||||
'psgi.input' => $client,
|
||||
'psgi.errors' => *STDERR,
|
||||
'psgi.multithread' => Plack::Util::FALSE,
|
||||
'psgi.multiprocess' => Plack::Util::TRUE,
|
||||
'psgi.run_once' => Plack::Util::FALSE,
|
||||
'psgi.streaming' => Plack::Util::TRUE,
|
||||
'psgi.nonblocking' => Plack::Util::FALSE,
|
||||
};
|
||||
|
||||
my $res = Plack::Util::run_app $app, $env;
|
||||
|
||||
if (ref $res eq 'ARRAY') {
|
||||
$self->_handle_response($client, $env{'SERVER_PROTOCOL'}, $res);
|
||||
}
|
||||
elsif (ref $res eq 'CODE') {
|
||||
$res->(sub {
|
||||
$self->_handle_response($client, $env{'SERVER_PROTOCOL'}, $_[0]);
|
||||
});
|
||||
}
|
||||
else {
|
||||
die "Bad response $res";
|
||||
}
|
||||
|
||||
$client->close ;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sub _handle_response {
|
||||
my ($self, $client, $protocol, $res) = @_;
|
||||
|
||||
$client->send($protocol.' '.$res->[0].' '.HTTP::Status::status_message( $res->[0] )."\r\n");
|
||||
|
||||
my $headers = $res->[1];
|
||||
my $hdrs = '';
|
||||
while (my ($k, $v) = splice @$headers, 0, 2) {
|
||||
$hdrs .= "$k: $v\r\n";
|
||||
}
|
||||
$hdrs .= "\r\n";
|
||||
|
||||
$client->send($hdrs);
|
||||
|
||||
my $cb = sub { $client->send($_[0]) };
|
||||
my $body = $res->[2];
|
||||
if (defined $body) {
|
||||
Plack::Util::foreach($body, $cb);
|
||||
}
|
||||
else {
|
||||
return Plack::Util::inline_object write => $cb,close => sub { };
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
@@ -0,0 +1,15 @@
|
||||
# uWSGI - manage uWSGI application server
|
||||
#
|
||||
|
||||
description "uWSGI"
|
||||
|
||||
start on runlevel [2345]
|
||||
stop on runlevel [!2345]
|
||||
|
||||
respawn
|
||||
|
||||
env LOGTO=/tmp/uwsgi.log
|
||||
env BINPATH=/home/foo/uwsgi/uwsgi
|
||||
|
||||
# --no-orphans avoid workers without master
|
||||
exec $BINPATH --no-orphans --socket :3033 --uid 1001 --gid 1001 --limit-as 256 -m simple_app --logto $LOGTO -M -p 4
|
||||
@@ -0,0 +1,311 @@
|
||||
require 'rubygems'
|
||||
require 'socket'
|
||||
require 'rack'
|
||||
require 'rack/content_length'
|
||||
require 'rack/rewindable_input'
|
||||
require 'stringio'
|
||||
|
||||
require 'optparse'
|
||||
|
||||
options = {
|
||||
:processes => 1,
|
||||
:master => false,
|
||||
:logging => true,
|
||||
:static => true,
|
||||
:config => nil,
|
||||
:cachehack => false
|
||||
}
|
||||
|
||||
opts = OptionParser.new do |opts|
|
||||
|
||||
opts.on("-p", "--processes=nproc", Integer, '') { |p| options[:processes] = p }
|
||||
opts.on("-M", "--master", '') { options[:master] = true }
|
||||
opts.on("-L", "--no-logging", '') { options[:logging] = false }
|
||||
opts.on("-S", "--no-static", '') { options[:static] = false }
|
||||
opts.on("-C", "--cache-hack", '') { options[:cachehack] = true }
|
||||
|
||||
opts.parse! ARGV
|
||||
end
|
||||
|
||||
if ARGV[0]
|
||||
options[:config] = ARGV[0]
|
||||
end
|
||||
|
||||
def human_round(float)
|
||||
(float * (10 ** 2)).round / (10 ** 2).to_f
|
||||
end
|
||||
|
||||
$requests = 0
|
||||
$workers = Array.new
|
||||
|
||||
module Rack
|
||||
|
||||
class RailsCachingHack
|
||||
def initialize app
|
||||
@app = app
|
||||
end
|
||||
|
||||
def stripuri(uri)
|
||||
uri = uri.split('/')
|
||||
uri.slice!(1)
|
||||
val = uri.join('/')
|
||||
end
|
||||
def call env
|
||||
env['PATH_INFO'] = stripuri(env['PATH_INFO']) if env['PATH_INFO']
|
||||
env['REQUEST_URI'] = stripuri(env['REQUEST_URI']) if env['REQUEST_URI']
|
||||
env['REQUEST_URI'] = '/' if env['REQUEST_URI'] == ''
|
||||
@app.call env
|
||||
end
|
||||
end
|
||||
|
||||
module Handler
|
||||
class Unbit
|
||||
|
||||
def self.run(app, options={})
|
||||
server = UNIXServer.for_fd(0)
|
||||
|
||||
can_spawn = true
|
||||
if options[:master]
|
||||
$0 = 'uRack master'
|
||||
$workers[0] = Process.pid
|
||||
ulog("master process enabled (pid: #{$workers[0]})")
|
||||
$workers[1] = Process.fork
|
||||
if $workers[1].to_i > 0
|
||||
ulog("spawned worker 1 (pid: #{$workers[1]})")
|
||||
elsif $workers[1].to_i == 0
|
||||
$0 = "uRack worker 1"
|
||||
can_spawn = false
|
||||
end
|
||||
else
|
||||
$0 = 'uRack worker 1'
|
||||
$workers[0] = nil
|
||||
$workers[1] = Process.pid
|
||||
ulog("spawned worker 1 (pid: #{$workers[1]})")
|
||||
end
|
||||
|
||||
if options[:processes] > 1 and can_spawn
|
||||
for p in 2..options[:processes]
|
||||
$workers[p] = Process.fork
|
||||
if $workers[p].to_i == 0
|
||||
$0 = "uRack worker #{p}"
|
||||
break
|
||||
elsif $workers[p].to_i > 0
|
||||
ulog("spawned worker #{p} (pid: #{$workers[p]})")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# am i the master ?
|
||||
if options[:master] and Process.pid == $workers[0]
|
||||
while 1
|
||||
i_am_a_child = false
|
||||
pid = Process.waitpid
|
||||
for wid in 1..options[:processes]
|
||||
if $workers[wid] == pid
|
||||
ulog("worker #{wid} died ! (pid: #{pid})")
|
||||
$workers[wid] = Process.fork
|
||||
if $workers[wid].to_i == 0
|
||||
$0 = "uRack worker #{wid}"
|
||||
i_am_a_child = true
|
||||
break
|
||||
elsif $workers[wid].to_i > 0
|
||||
ulog("respawned worker #{wid} (pid: #{$workers[wid]})")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
break if i_am_a_child
|
||||
end
|
||||
end
|
||||
|
||||
while client = server.accept
|
||||
serve client, app, options
|
||||
end
|
||||
end
|
||||
|
||||
def self.serve(client, app, options)
|
||||
|
||||
speed = Time.now
|
||||
head, sender = client.recvfrom(4)
|
||||
|
||||
unless head
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
mod1, size, mod2 = head.unpack('CvC')
|
||||
|
||||
if size == 0 or size.nil?
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
vars, sender = client.recvfrom(size)
|
||||
|
||||
if vars.length != size
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
env = Hash.new
|
||||
|
||||
i = 0
|
||||
while i < size
|
||||
kl = vars[i, 2].unpack('v')[0]
|
||||
i = i + 2
|
||||
key = vars[i, kl]
|
||||
i = i + kl
|
||||
vl = vars[i, 2].unpack('v')[0]
|
||||
i = i + 2
|
||||
value = vars[i, vl]
|
||||
i = i + vl
|
||||
env[key] = value
|
||||
end
|
||||
|
||||
|
||||
|
||||
env.delete "HTTP_CONTENT_LENGTH"
|
||||
env.delete "HTTP_CONTENT_TYPE"
|
||||
|
||||
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
|
||||
env["QUERY_STRING"] ||= ""
|
||||
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
|
||||
env["REQUEST_PATH"] ||= "/"
|
||||
env.delete "PATH_INFO" if env["PATH_INFO"] == ""
|
||||
env.delete "CONTENT_TYPE" if env["CONTENT_TYPE"] == ""
|
||||
env.delete "CONTENT_LENGTH" if env["CONTENT_LENGTH"] == ""
|
||||
|
||||
|
||||
if env["CONTENT_LENGTH"].to_i > 4096
|
||||
rack_input = Rack::RewindableInput::Tempfile.new('Rack_unbit_Input')
|
||||
rack_input.chmod(0000)
|
||||
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
|
||||
rack_input.binmode
|
||||
rack_input.unlink
|
||||
|
||||
remains = env["CONTENT_LENGTH"].to_i
|
||||
while remains > 0
|
||||
if remains >= 4096
|
||||
buf, sender = client.recvfrom(4096)
|
||||
else
|
||||
buf, sender = client.recvfrom(remains)
|
||||
end
|
||||
|
||||
if buf.length == 0
|
||||
break
|
||||
end
|
||||
|
||||
rack_input.write( buf )
|
||||
remains -= buf.length
|
||||
end
|
||||
|
||||
elsif env["CONTENT_LENGTH"].to_i > 0
|
||||
rack_input = StringIO.new(client.recvfrom(env["CONTENT_LENGTH"].to_i)[0])
|
||||
else
|
||||
rack_input = StringIO.new('')
|
||||
end
|
||||
|
||||
rack_input.rewind
|
||||
|
||||
env.update({"rack.version" => [1,1],
|
||||
"rack.input" => rack_input,
|
||||
"rack.errors" => $stderr,
|
||||
|
||||
"rack.multithread" => false,
|
||||
"rack.multiprocess" => true,
|
||||
"rack.run_once" => false,
|
||||
|
||||
"rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
|
||||
})
|
||||
|
||||
app = Rack::ContentLength.new(app)
|
||||
|
||||
disconnected = false
|
||||
begin
|
||||
status, headers, body = app.call(env)
|
||||
begin
|
||||
send_headers client, env["HTTP_VERSION"] ,status, headers
|
||||
send_body client, body
|
||||
ensure
|
||||
body.close if body.respond_to? :close
|
||||
end
|
||||
rescue Errno::EPIPE, Errno::ECONNRESET
|
||||
disconnected = true
|
||||
ensure
|
||||
rack_input.close
|
||||
client.close
|
||||
end
|
||||
$requests = $requests+1
|
||||
|
||||
ulog("req: #{$requests} ip: #{env['REMOTE_ADDR']} pid: #{Process.pid} as: #{human_round(syscall(356).to_f/1024/1024)} MB => #{env['REQUEST_METHOD']} #{env['REQUEST_URI']} in #{Time.now-speed} secs [#{status}]#{' DISCONNECTED !!!' if disconnected}") if options[:logging]
|
||||
end
|
||||
|
||||
def self.send_headers(client, protocol, status, headers)
|
||||
client.print "#{protocol} #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}\r\n"
|
||||
headers.each { |k, vs|
|
||||
vs.split("\n").each { |v|
|
||||
client.print "#{k}: #{v}\r\n"
|
||||
}
|
||||
}
|
||||
client.print "\r\n"
|
||||
client.flush
|
||||
end
|
||||
|
||||
def self.send_body(client, body)
|
||||
body.each { |part|
|
||||
client.print part
|
||||
client.flush
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def ulog(message)
|
||||
$stderr.puts "[#{Time.new}] uRack: #{message}"
|
||||
end
|
||||
|
||||
ulog("starting at #{Dir.getwd}")
|
||||
limit_as = human_round(Process.getrlimit(Process::RLIMIT_AS)[1].to_f/1024/1024)
|
||||
ulog("your process address space limit is #{limit_as} MB")
|
||||
server = Rack::Handler.get('Unbit')
|
||||
|
||||
starttime = Time.now
|
||||
|
||||
app = nil
|
||||
|
||||
if options[:config]
|
||||
cfgfile = File.read(options[:config])
|
||||
if cfgfile[/^#\\(.*)/]
|
||||
opts.parse! $1.split(/\s+/)
|
||||
end
|
||||
app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app", nil, options[:config]
|
||||
else
|
||||
# falling back to rails
|
||||
if Dir.getwd =~ /\/public\/?$/
|
||||
require '../config/environment'
|
||||
else
|
||||
require 'config/environment'
|
||||
end
|
||||
app = Rack::Builder.new {
|
||||
if options[:cachehack]
|
||||
use Rack::RailsCachingHack
|
||||
end
|
||||
if options[:static]
|
||||
use Rails::Rack::Static
|
||||
end
|
||||
if ActionController.const_defined?(:Dispatcher) && (ActionController::Dispatcher.instance_methods.include?(:call) || ActionController::Dispatcher.instance_methods.include?("call"))
|
||||
run ActionController::Dispatcher.new
|
||||
else
|
||||
require 'thin'
|
||||
run Rack::Adapter::Rails.new(:environment => ENV['RAILS_ENV'])
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
ulog("your app is ready (in #{Time.now-starttime} seconds).")
|
||||
|
||||
|
||||
server.run(app, options)
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
require 'socket'
|
||||
require 'rack/content_length'
|
||||
require 'rack/rewindable_input'
|
||||
require 'stringio'
|
||||
|
||||
module Rack
|
||||
module Handler
|
||||
class Uwsgi
|
||||
|
||||
def self.run(app, options={})
|
||||
if ENV['UWSGI_FD']
|
||||
server = UNIXServer.for_fd(ENV['UWSGI_FD'].to_i)
|
||||
else
|
||||
server = TCPServer.new(options[:Host], options[:Port])
|
||||
end
|
||||
while client = server.accept
|
||||
serve client, app
|
||||
end
|
||||
end
|
||||
|
||||
def self.serve(client, app)
|
||||
|
||||
head, sender = client.recvfrom(4)
|
||||
|
||||
unless head
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
mod1, size, mod2 = head.unpack('CvC')
|
||||
|
||||
if size == 0 or size.nil?
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
vars, sender = client.recvfrom(size)
|
||||
|
||||
if vars.length != size
|
||||
client.close
|
||||
return
|
||||
end
|
||||
|
||||
env = Hash.new
|
||||
|
||||
i = 0
|
||||
while i < size
|
||||
kl = vars[i, 2].unpack('v')[0]
|
||||
i = i + 2
|
||||
key = vars[i, kl]
|
||||
i = i + kl
|
||||
vl = vars[i, 2].unpack('v')[0]
|
||||
i = i + 2
|
||||
value = vars[i, vl]
|
||||
i = i + vl
|
||||
env[key] = value
|
||||
end
|
||||
|
||||
|
||||
|
||||
env.delete "HTTP_CONTENT_LENGTH"
|
||||
env.delete "HTTP_CONTENT_TYPE"
|
||||
|
||||
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
|
||||
env["QUERY_STRING"] ||= ""
|
||||
env["HTTP_VERSION"] ||= env["SERVER_PROTOCOL"]
|
||||
env["REQUEST_PATH"] ||= "/"
|
||||
env.delete "PATH_INFO" if env["PATH_INFO"] == ""
|
||||
env.delete "CONTENT_TYPE" if env["CONTENT_TYPE"] == ""
|
||||
env.delete "CONTENT_LENGTH" if env["CONTENT_LENGTH"] == ""
|
||||
|
||||
|
||||
if env["CONTENT_LENGTH"].to_i > 4096
|
||||
rack_input = Rack::RewindableInput::Tempfile.new('Rack_uwsgi_Input')
|
||||
rack_input.chmod(0000)
|
||||
rack_input.set_encoding(Encoding::BINARY) if rack_input.respond_to?(:set_encoding)
|
||||
rack_input.binmode
|
||||
if RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/
|
||||
rack_input.unlink
|
||||
end
|
||||
|
||||
remains = env["CONTENT_LENGTH"].to_i
|
||||
while remains > 0
|
||||
if remains >= 4096
|
||||
buf, sender = client.recvfrom(4096)
|
||||
else
|
||||
buf, sender = client.recvfrom(remains)
|
||||
end
|
||||
|
||||
rack_input.write( buf )
|
||||
remains -= buf.length
|
||||
end
|
||||
|
||||
elsif env["CONTENT_LENGTH"].to_i > 0
|
||||
rack_input = StringIO.new(client.recvfrom(env["CONTENT_LENGTH"].to_i)[0])
|
||||
else
|
||||
rack_input = StringIO.new('')
|
||||
end
|
||||
|
||||
rack_input.rewind
|
||||
|
||||
env.update({"rack.version" => [1,1],
|
||||
"rack.input" => rack_input,
|
||||
"rack.errors" => $stderr,
|
||||
|
||||
"rack.multithread" => false,
|
||||
"rack.multiprocess" => true,
|
||||
"rack.run_once" => false,
|
||||
|
||||
"rack.url_scheme" => ["yes", "on", "1"].include?(env["HTTPS"]) ? "https" : "http"
|
||||
})
|
||||
|
||||
|
||||
app = Rack::ContentLength.new(app)
|
||||
|
||||
begin
|
||||
status, headers, body = app.call(env)
|
||||
begin
|
||||
send_headers client, env["HTTP_VERSION"] ,status, headers
|
||||
send_body client, body
|
||||
ensure
|
||||
body.close if body.respond_to? :close
|
||||
end
|
||||
rescue Errno::EPIPE, Errno::ECONNRESET
|
||||
ensure
|
||||
rack_input.close
|
||||
client.close
|
||||
end
|
||||
end
|
||||
|
||||
def self.send_headers(client, protocol, status, headers)
|
||||
client.print "#{protocol} #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}\r\n"
|
||||
headers.each { |k, vs|
|
||||
vs.split("\n").each { |v|
|
||||
client.print "#{k}: #{v}\r\n"
|
||||
}
|
||||
}
|
||||
client.print "\r\n"
|
||||
client.flush
|
||||
end
|
||||
|
||||
def self.send_body(client, body)
|
||||
body.each { |part|
|
||||
client.print part
|
||||
client.flush
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Vendored
-6
@@ -1,6 +0,0 @@
|
||||
Package: uwsgi
|
||||
Version: 0.9.2
|
||||
Maintainer: Unbit
|
||||
Description: Fast, developer-friendly wsgi server
|
||||
Architecture: i386
|
||||
Depends: python2.5, libxml2
|
||||
@@ -0,0 +1,70 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
|
||||
{% load i18n %}
|
||||
|
||||
|
||||
{% block breadcrumbs %}
|
||||
<div class="breadcrumbs">
|
||||
<a href="../">{% trans 'Home' %}</a> ›
|
||||
{% trans 'uWSGI' %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
<div id="content-main">
|
||||
<h1>uWSGI status</h1>
|
||||
<b>masterpid:</b> {{masterpid}}<br/>
|
||||
<b>started_on:</b> {{started_on}}<br/>
|
||||
<b>buffer size:</b> {{buffer_size}}<br/>
|
||||
<b>workers:</b> {{numproc}}<br/>
|
||||
<b>total requests:</b> {{total_requests}}<br/>
|
||||
|
||||
<br/>
|
||||
|
||||
<div class="module" style="width:860px">
|
||||
<table>
|
||||
<caption style="width:860px">workers</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="row">id</th>
|
||||
<th scope="row">pid</th>
|
||||
<th scope="row">requests</th>
|
||||
<th scope="row">running time (milliseconds)</th>
|
||||
<th scope="row">load</th>
|
||||
<th scope="row">last spawn</th>
|
||||
<th scope="row">respawn count</th>
|
||||
<th scope="row">address space (vsz)</th>
|
||||
<th scope="row">resident memory (rss)</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for w in workers %}
|
||||
<tr>
|
||||
<td align="center">{{w.id}}</td>
|
||||
<td align="center">{{w.pid}}</td>
|
||||
<td align="right">{{w.requests}}</td>
|
||||
<td align="right">{{w.running_time}}</td>
|
||||
<td align="right">{{w.load|floatformat:2}} %</td>
|
||||
<td align="right">{{w.last_spawn_str}}</td>
|
||||
<td align="right">{{w.respawn_count}}</td>
|
||||
<td align="right">{{w.vsz|filesizeformat}}</td>
|
||||
<td align="right">{{w.rss|filesizeformat}}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{% if masterpid %}
|
||||
<br/>
|
||||
<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="reload/" method="post">
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="gracefully reload uWSGI" name="_reload"/>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from django.conf.urls.defaults import *
|
||||
|
||||
urlpatterns = patterns('uwsgi_admin.views',
|
||||
(r'^$', 'index'),
|
||||
(r'^reload/$', 'reload')
|
||||
)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import uwsgi
|
||||
import time
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpResponseRedirect
|
||||
|
||||
|
||||
def index(request):
|
||||
workers = uwsgi.workers()
|
||||
total_load = time.time() - uwsgi.started_on
|
||||
for w in workers:
|
||||
w['load'] = (100 * (w['running_time']/1000))/total_load
|
||||
w['last_spawn_str'] = time.ctime(w['last_spawn'])
|
||||
|
||||
return render_to_response('uwsgi.html', {'masterpid': uwsgi.masterpid(),
|
||||
'started_on': time.ctime(uwsgi.started_on),
|
||||
'buffer_size': uwsgi.buffer_size,
|
||||
'total_requests': uwsgi.total_requests(),
|
||||
'numproc': uwsgi.numproc,
|
||||
'workers': workers,
|
||||
}, RequestContext(request, {}))
|
||||
index = staff_member_required(index)
|
||||
|
||||
def reload(request):
|
||||
if uwsgi.masterpid() > 0:
|
||||
uwsgi.reload()
|
||||
request.user.message_set.create(message="uWSGI reloaded")
|
||||
else:
|
||||
request.user.message_set.create(message="The uWSGI master process is not active")
|
||||
|
||||
return HttpResponseRedirect(reverse(index))
|
||||
|
||||
reload = staff_member_required(reload)
|
||||
@@ -0,0 +1,702 @@
|
||||
#ifdef UWSGI_ERLANG
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
static void erlang_log(void);
|
||||
|
||||
PyObject *py_erlang_connect(PyObject * self, PyObject * args) {
|
||||
|
||||
char *erlang_node;
|
||||
int fd;
|
||||
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s:erlang_connect", &erlang_node)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UWSGI_SET_BLOCKING;
|
||||
fd = erl_connect(erlang_node);
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
return PyInt_FromLong(fd);
|
||||
}
|
||||
|
||||
PyObject *py_erlang_recv_message(PyObject * self, PyObject * args) {
|
||||
int erfd;
|
||||
struct pollfd erpoll;
|
||||
ErlMessage emsg;
|
||||
PyObject *pyer = NULL;
|
||||
unsigned char erlang_buffer[8192];
|
||||
int eret;
|
||||
int timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i|i:erlang_recv_message", &erfd, &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (erfd < 0) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
erpoll.fd = erfd;
|
||||
erpoll.events = POLLIN;
|
||||
cycle:
|
||||
memset(&emsg, 0, sizeof(ErlMessage));
|
||||
UWSGI_SET_BLOCKING;
|
||||
if (timeout > 0) {
|
||||
eret = poll(&erpoll, 1, timeout * 1000);
|
||||
if (eret < 0) {
|
||||
uwsgi_error("poll()");
|
||||
goto clear;
|
||||
}
|
||||
else if (eret == 0) {
|
||||
goto clear;
|
||||
}
|
||||
}
|
||||
if (erl_receive_msg(erfd, erlang_buffer, 8192, &emsg) == ERL_MSG) {
|
||||
if (emsg.type == ERL_TICK) {
|
||||
goto cycle;
|
||||
}
|
||||
if (emsg.msg) {
|
||||
pyer = eterm_to_py(emsg.msg);
|
||||
}
|
||||
if (emsg.msg) {
|
||||
erl_free_compound(emsg.msg);
|
||||
}
|
||||
if (emsg.to) {
|
||||
erl_free_compound(emsg.to);
|
||||
}
|
||||
if (emsg.from) {
|
||||
erl_free_compound(emsg.from);
|
||||
}
|
||||
if (!pyer) {
|
||||
goto clear;
|
||||
}
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
return pyer;
|
||||
}
|
||||
|
||||
clear:
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_erlang_send_message(PyObject * self, PyObject * args) {
|
||||
|
||||
ETERM *pymessage;
|
||||
ETERM *pid;
|
||||
int erfd;
|
||||
PyObject *ermessage, *erdest, *zero;
|
||||
|
||||
int er_number, er_serial, er_creation;
|
||||
char *er_node;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "iOO|i:erlang_send_message", &erfd, &erdest, &ermessage)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (erfd < 0) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
if (!PyString_Check(erdest) && !PyDict_Check(erdest)) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
pymessage = py_to_eterm(ermessage);
|
||||
if (!pymessage) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
|
||||
if (PyString_Check(erdest)) {
|
||||
if (!erl_reg_send(erfd, PyString_AsString(erdest), pymessage)) {
|
||||
erl_err_msg("erl_reg_send()");
|
||||
goto clear2;
|
||||
}
|
||||
}
|
||||
else if (PyDict_Check(erdest)) {
|
||||
zero = PyDict_GetItemString(erdest, "node");
|
||||
if (!zero) {
|
||||
goto clear2;
|
||||
}
|
||||
if (!PyString_Check(zero)) {
|
||||
goto clear2;
|
||||
}
|
||||
er_node = PyString_AsString(zero);
|
||||
|
||||
zero = PyDict_GetItemString(erdest, "number");
|
||||
if (!zero) {
|
||||
goto clear2;
|
||||
}
|
||||
if (!PyInt_Check(zero)) {
|
||||
goto clear2;
|
||||
}
|
||||
er_number = PyInt_AsLong(zero);
|
||||
|
||||
zero = PyDict_GetItemString(erdest, "serial");
|
||||
if (!zero) {
|
||||
goto clear2;
|
||||
}
|
||||
if (!PyInt_Check(zero)) {
|
||||
goto clear2;
|
||||
}
|
||||
er_serial = PyInt_AsLong(zero);
|
||||
|
||||
zero = PyDict_GetItemString(erdest, "creation");
|
||||
if (!zero) {
|
||||
goto clear2;
|
||||
}
|
||||
if (!PyInt_Check(zero)) {
|
||||
goto clear2;
|
||||
}
|
||||
er_creation = PyInt_AsLong(zero);
|
||||
|
||||
pid = erl_mk_pid((const char *) er_node, er_number, er_serial, er_creation);
|
||||
|
||||
if (!pid) {
|
||||
goto clear2;
|
||||
}
|
||||
|
||||
if (!erl_send(erfd, pid, pymessage)) {
|
||||
erl_err_msg("erl_send()");
|
||||
erl_free_term(pid);
|
||||
goto clear2;
|
||||
}
|
||||
|
||||
erl_free_term(pid);
|
||||
}
|
||||
else {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
erl_free_compound(pymessage);
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
clear2:
|
||||
erl_free_compound(pymessage);
|
||||
clear:
|
||||
PyErr_Print();
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_erlang_rpc(PyObject * self, PyObject * args) {
|
||||
|
||||
int fd, timeout = uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT];
|
||||
char *emod, *efun;
|
||||
PyObject *eargs, *pyer = NULL;
|
||||
ETERM *pyargs, *rex;
|
||||
ErlMessage emsg;
|
||||
int eret;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "issO|i:erlang_rpc", &fd, &emod, &efun, &eargs, &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
pyargs = py_to_eterm(eargs);
|
||||
if (!pyargs) {
|
||||
goto clear;
|
||||
}
|
||||
|
||||
|
||||
if (erl_rpc_to(fd, emod, efun, pyargs)) {
|
||||
erl_err_msg("erl_rpc_to()");
|
||||
goto clear2;
|
||||
}
|
||||
|
||||
|
||||
cycle:
|
||||
memset(&emsg, 0, sizeof(ErlMessage));
|
||||
UWSGI_SET_BLOCKING;
|
||||
eret = erl_rpc_from(fd, timeout * 1000, &emsg);
|
||||
if (eret == ERL_TICK) {
|
||||
goto cycle;
|
||||
}
|
||||
else if (eret == ERL_MSG) {
|
||||
if (emsg.msg) {
|
||||
if (ERL_IS_TUPLE(emsg.msg)) {
|
||||
rex = erl_element(1, emsg.msg);
|
||||
if (!rex) {
|
||||
goto clear2;
|
||||
}
|
||||
if (!strncmp("rex", ERL_ATOM_PTR(rex), ERL_ATOM_SIZE(rex))) {
|
||||
erl_free_term(rex);
|
||||
rex = erl_element(2, emsg.msg);
|
||||
if (!rex) {
|
||||
goto clear2;
|
||||
}
|
||||
pyer = eterm_to_py(rex);
|
||||
erl_free_term(rex);
|
||||
}
|
||||
else {
|
||||
erl_free_term(rex);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (emsg.msg) {
|
||||
erl_free_compound(emsg.msg);
|
||||
}
|
||||
if (emsg.to) {
|
||||
erl_free_compound(emsg.to);
|
||||
}
|
||||
if (emsg.from) {
|
||||
erl_free_compound(emsg.from);
|
||||
}
|
||||
if (!pyer) {
|
||||
goto clear2;
|
||||
}
|
||||
erl_free_compound(pyargs);
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
return pyer;
|
||||
}
|
||||
else {
|
||||
erl_err_msg("erl_rpc_from()");
|
||||
}
|
||||
|
||||
clear2:
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
erl_free_compound(pyargs);
|
||||
|
||||
clear:
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_erlang_close(PyObject * self, PyObject * args) {
|
||||
|
||||
int fd;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i:erlang_close", &fd)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
if (fd >= 0) {
|
||||
erl_close_connection(fd);
|
||||
}
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
static PyMethodDef uwsgi_erlang_methods[] = {
|
||||
{"erlang_connect", py_erlang_connect, METH_VARARGS, ""},
|
||||
{"erlang_send_message", py_erlang_send_message, METH_VARARGS, ""},
|
||||
{"erlang_recv_message", py_erlang_recv_message, METH_VARARGS, ""},
|
||||
{"erlang_rpc", py_erlang_rpc, METH_VARARGS, ""},
|
||||
{"erlang_close", py_erlang_close, METH_VARARGS, ""},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
|
||||
int init_erlang(char *nodename, char *cookie) {
|
||||
|
||||
struct sockaddr_in e_addr;
|
||||
|
||||
char *ip;
|
||||
char *node;
|
||||
int efd;
|
||||
int rlen;
|
||||
char *cookiefile;
|
||||
char *cookiehome;
|
||||
char cookievalue[128];
|
||||
int cookiefd;
|
||||
|
||||
PyMethodDef *uwsgi_function;
|
||||
|
||||
|
||||
ip = strchr(nodename, '@');
|
||||
|
||||
if (ip == NULL) {
|
||||
uwsgi_log( "*** invalid erlang node name ***\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cookie == NULL) {
|
||||
// get the cookie from the home
|
||||
cookiehome = getenv("HOME");
|
||||
if (!cookiehome) {
|
||||
uwsgi_log( "unable to get erlang cookie from your home.\n");
|
||||
return -1;
|
||||
}
|
||||
cookiefile = malloc(strlen(cookiehome) + 1 + strlen(".erlang.cookie") + 1);
|
||||
if (!cookiefile) {
|
||||
uwsgi_error("malloc()");
|
||||
}
|
||||
cookiefile[0] = 0;
|
||||
strcat(cookiefile, cookiehome);
|
||||
strcat(cookiefile, "/.erlang.cookie");
|
||||
|
||||
cookiefd = open(cookiefile, O_RDONLY);
|
||||
if (cookiefd < 0) {
|
||||
uwsgi_error("open()");
|
||||
free(cookiefile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(cookievalue, 0, 128);
|
||||
if (read(cookiefd, cookievalue, 127) < 1) {
|
||||
uwsgi_log( "invalid cookie found in %s\n", cookiefile);
|
||||
close(cookiefd);
|
||||
free(cookiefile);
|
||||
return -1;
|
||||
}
|
||||
cookie = cookievalue;
|
||||
close(cookiefd);
|
||||
free(cookiefile);
|
||||
}
|
||||
|
||||
node = malloc((ip - nodename) + 1);
|
||||
if (node == NULL) {
|
||||
uwsgi_error("malloc()");
|
||||
return -1;
|
||||
}
|
||||
memset(node, 0, (ip - nodename) + 1);
|
||||
memcpy(node, nodename, ip - nodename);
|
||||
|
||||
erl_init(NULL, 0);
|
||||
|
||||
if (erl_connect_xinit(ip + 1, node, nodename, NULL, cookie, 0) == -1) {
|
||||
uwsgi_log( "*** unable to initialize erlang c-node ***\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
efd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (efd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
memset(&e_addr, 0, sizeof(struct sockaddr_in));
|
||||
e_addr.sin_family = AF_INET;
|
||||
e_addr.sin_addr.s_addr = inet_addr(ip + 1);
|
||||
|
||||
rlen = 1;
|
||||
if (setsockopt(efd, SOL_SOCKET, SO_REUSEADDR, &rlen, sizeof(rlen))) {
|
||||
uwsgi_error("setsockopt()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bind(efd, (struct sockaddr *) &e_addr, sizeof(struct sockaddr_in)) < 0) {
|
||||
uwsgi_error("bind()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
rlen = sizeof(struct sockaddr_in);
|
||||
if (getsockname(efd, (struct sockaddr *) &e_addr, (socklen_t *) & rlen)) {
|
||||
uwsgi_error("getsockname()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (listen(efd, uwsgi.listen_queue)) {
|
||||
uwsgi_error("listen()");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (erl_publish(ntohs(e_addr.sin_port)) < 0) {
|
||||
uwsgi_log( "*** unable to subscribe with EPMD ***\n");
|
||||
close(efd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_log( "Erlang C-Node initialized on port %d you can access it with name %s\n", ntohs(e_addr.sin_port), nodename);
|
||||
|
||||
for (uwsgi_function = uwsgi_erlang_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) {
|
||||
PyObject *func = PyCFunction_New(uwsgi_function, NULL);
|
||||
PyDict_SetItemString(uwsgi.embedded_dict, uwsgi_function->ml_name, func);
|
||||
Py_DECREF(func);
|
||||
}
|
||||
|
||||
return efd;
|
||||
|
||||
}
|
||||
|
||||
ETERM *py_to_eterm(PyObject * pobj) {
|
||||
int i;
|
||||
int count;
|
||||
PyObject *pobj2;
|
||||
ETERM *eobj = NULL;
|
||||
ETERM *eobj2 = NULL;
|
||||
ETERM **eobj3;
|
||||
|
||||
if (pobj == NULL) {
|
||||
return erl_mk_empty_list();
|
||||
}
|
||||
|
||||
if (PyString_Check(pobj)) {
|
||||
eobj = erl_mk_atom(PyString_AsString(pobj));
|
||||
}
|
||||
else if (PyInt_Check(pobj)) {
|
||||
eobj = erl_mk_int(PyInt_AsLong(pobj));
|
||||
}
|
||||
else if (PyList_Check(pobj)) {
|
||||
eobj = erl_mk_empty_list();
|
||||
for (i = PyList_Size(pobj) - 1; i >= 0; i--) {
|
||||
pobj2 = PyList_GetItem(pobj, i);
|
||||
eobj2 = py_to_eterm(pobj2);
|
||||
eobj = erl_cons(eobj2, eobj);
|
||||
}
|
||||
}
|
||||
else if (PyDict_Check(pobj)) {
|
||||
// a pid
|
||||
char *er_node;
|
||||
int er_number, er_serial, er_creation;
|
||||
pobj2 = PyDict_GetItemString(pobj, "node");
|
||||
if (!pobj2) {
|
||||
PyErr_Print();
|
||||
goto clear;
|
||||
}
|
||||
if (!PyString_Check(pobj2)) {
|
||||
goto clear;
|
||||
}
|
||||
er_node = PyString_AsString(pobj2);
|
||||
|
||||
pobj2 = PyDict_GetItemString(pobj, "number");
|
||||
if (!pobj2) {
|
||||
PyErr_Print();
|
||||
goto clear;
|
||||
}
|
||||
if (!PyInt_Check(pobj2)) {
|
||||
goto clear;
|
||||
}
|
||||
er_number = PyInt_AsLong(pobj2);
|
||||
|
||||
pobj2 = PyDict_GetItemString(pobj, "serial");
|
||||
if (!pobj2) {
|
||||
PyErr_Print();
|
||||
goto clear;
|
||||
}
|
||||
if (!PyInt_Check(pobj2)) {
|
||||
goto clear;
|
||||
}
|
||||
er_serial = PyInt_AsLong(pobj2);
|
||||
|
||||
pobj2 = PyDict_GetItemString(pobj, "creation");
|
||||
if (!pobj2) {
|
||||
PyErr_Print();
|
||||
goto clear;
|
||||
}
|
||||
if (!PyInt_Check(pobj2)) {
|
||||
goto clear;
|
||||
}
|
||||
er_creation = PyInt_AsLong(pobj2);
|
||||
|
||||
eobj = erl_mk_pid(er_node, er_number, er_serial, er_creation);
|
||||
}
|
||||
else if (PyTuple_Check(pobj)) {
|
||||
count = PyTuple_Size(pobj);
|
||||
eobj3 = malloc(sizeof(ETERM *) * count);
|
||||
for (i = 0; i < count; i++) {
|
||||
pobj2 = PyTuple_GetItem(pobj, i);
|
||||
if (!pobj2) {
|
||||
break;
|
||||
}
|
||||
eobj3[i] = py_to_eterm(pobj2);
|
||||
}
|
||||
eobj = erl_mk_tuple(eobj3, count);
|
||||
free(eobj3);
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "UNMANAGED PYTHON TYPE: %s\n", pobj->ob_type->tp_name);
|
||||
}
|
||||
|
||||
clear:
|
||||
if (eobj == NULL) {
|
||||
return erl_mk_empty_list();
|
||||
}
|
||||
|
||||
return eobj;
|
||||
}
|
||||
|
||||
PyObject *eterm_to_py(ETERM * obj) {
|
||||
int i;
|
||||
int count;
|
||||
ETERM *obj2;
|
||||
PyObject *eobj = NULL;
|
||||
|
||||
if (obj == NULL) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
switch (ERL_TYPE(obj)) {
|
||||
|
||||
case ERL_CONS:
|
||||
case ERL_NIL:
|
||||
count = erl_length(obj);
|
||||
eobj = PyList_New(0);
|
||||
for (i = 0; i < count; i++) {
|
||||
obj2 = erl_hd(obj);
|
||||
PyList_Append(eobj, eterm_to_py(obj2));
|
||||
obj = erl_tl(obj);
|
||||
}
|
||||
break;
|
||||
case ERL_TUPLE:
|
||||
eobj = PyTuple_New(erl_size(obj));
|
||||
for (i = 1; i <= erl_size(obj); i++) {
|
||||
obj2 = erl_element(i, obj);
|
||||
PyTuple_SetItem(eobj, i - 1, eterm_to_py(obj2));
|
||||
}
|
||||
break;
|
||||
case ERL_ATOM:
|
||||
eobj = PyString_FromStringAndSize(ERL_ATOM_PTR(obj), ERL_ATOM_SIZE(obj));
|
||||
break;
|
||||
case ERL_INTEGER:
|
||||
eobj = PyInt_FromLong(ERL_INT_VALUE(obj));
|
||||
break;
|
||||
case ERL_BINARY:
|
||||
uwsgi_log( "FOUND A BINARY %.*s\n", ERL_BIN_SIZE(obj), ERL_BIN_PTR(obj));
|
||||
break;
|
||||
case ERL_PID:
|
||||
eobj = PyDict_New();
|
||||
if (PyDict_SetItemString(eobj, "node", PyString_FromString(ERL_PID_NODE(obj)))) {
|
||||
PyErr_Print();
|
||||
break;
|
||||
}
|
||||
if (PyDict_SetItemString(eobj, "number", PyInt_FromLong(ERL_PID_NUMBER(obj)))) {
|
||||
PyErr_Print();
|
||||
break;
|
||||
}
|
||||
if (PyDict_SetItemString(eobj, "serial", PyInt_FromLong(ERL_PID_SERIAL(obj)))) {
|
||||
PyErr_Print();
|
||||
break;
|
||||
}
|
||||
if (PyDict_SetItemString(eobj, "creation", PyInt_FromLong(ERL_PID_CREATION(obj)))) {
|
||||
PyErr_Print();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
uwsgi_log( "UNMANAGED ETERM TYPE: %d\n", ERL_TYPE(obj));
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if (eobj == NULL) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
return eobj;
|
||||
}
|
||||
|
||||
void erlang_loop(struct wsgi_request *wsgi_req) {
|
||||
|
||||
ErlConnect econn;
|
||||
ErlMessage em;
|
||||
ETERM *eresponse;
|
||||
|
||||
PyObject *callable = PyDict_GetItemString(uwsgi.embedded_dict, "erlang_func");
|
||||
if (!callable) {
|
||||
PyErr_Print();
|
||||
uwsgi_log( "- you have not defined a uwsgi.erlang_func callable, Erlang message manager will be disabled until you define it -\n");
|
||||
}
|
||||
|
||||
PyObject *pargs = PyTuple_New(1);
|
||||
if (!pargs) {
|
||||
PyErr_Print();
|
||||
uwsgi_log( "- error preparing arg tuple for uwsgi.erlang_func callable, Erlang message manager will be disabled -\n");
|
||||
}
|
||||
|
||||
while (uwsgi.workers[uwsgi.mywid].manage_next_request) {
|
||||
|
||||
|
||||
UWSGI_CLEAR_STATUS;
|
||||
|
||||
wsgi_req->poll.fd = erl_accept(uwsgi.erlangfd, &econn);
|
||||
|
||||
if (wsgi_req->poll.fd >= 0) {
|
||||
|
||||
UWSGI_SET_ERLANGING;
|
||||
for (;;) {
|
||||
if (erl_receive_msg(wsgi_req->poll.fd, (unsigned char *) wsgi_req->buffer, uwsgi.buffer_size, &em) == ERL_MSG) {
|
||||
if (em.type == ERL_TICK)
|
||||
continue;
|
||||
|
||||
if (!callable) {
|
||||
callable = PyDict_GetItemString(uwsgi.embedded_dict, "erlang_func");
|
||||
}
|
||||
|
||||
if (!callable) {
|
||||
uwsgi_log( "- you still have not defined a uwsgi.erlang_func callable, Erlang message rejected -\n");
|
||||
}
|
||||
|
||||
PyObject *zero = eterm_to_py(em.msg);
|
||||
if (em.msg) {
|
||||
erl_free_compound(em.msg);
|
||||
}
|
||||
if (em.to) {
|
||||
erl_free_compound(em.to);
|
||||
}
|
||||
|
||||
if (!zero) {
|
||||
PyErr_Print();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (PyTuple_SetItem(pargs, 0, zero)) {
|
||||
PyErr_Print();
|
||||
continue;
|
||||
}
|
||||
|
||||
PyObject *erlang_result = PyEval_CallObject(callable, pargs);
|
||||
|
||||
//Py_DECREF(zero);
|
||||
|
||||
if (erlang_result) {
|
||||
eresponse = py_to_eterm(erlang_result);
|
||||
if (eresponse) {
|
||||
erl_send(wsgi_req->poll.fd, em.from, eresponse);
|
||||
erl_free_compound(eresponse);
|
||||
}
|
||||
Py_DECREF(erlang_result);
|
||||
}
|
||||
|
||||
if (em.from) {
|
||||
erl_free_compound(em.from);
|
||||
}
|
||||
|
||||
uwsgi.workers[0].requests++;
|
||||
uwsgi.workers[uwsgi.mywid].requests++;
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_LOGGING])
|
||||
erlang_log();
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
erl_close_connection(wsgi_req->poll.fd);
|
||||
|
||||
UWSGI_UNSET_ERLANGING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void erlang_log() {
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG]) {
|
||||
get_memusage();
|
||||
}
|
||||
else {
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = 0;
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = 0;
|
||||
}
|
||||
uwsgi_log( "[Erlang worker %d pid %d] request %llu done {rss: %llu vsz: %llu}\n", uwsgi.mywid, uwsgi.mypid, uwsgi.workers[uwsgi.mywid].requests, uwsgi.workers[uwsgi.mywid].rss_size, uwsgi.workers[uwsgi.mywid].vsz_size);
|
||||
}
|
||||
|
||||
#else
|
||||
#warning "*** Erlang support is disabled ***"
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifdef UWSGI_EVDIS
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
void evdis_loop(struct uwsgi_server *uwsgi) {
|
||||
|
||||
// populate event list
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#warning "*** Event Dispatcher support is disabled ***"
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#ifdef __linux__
|
||||
|
||||
#include <sys/inotify.h>
|
||||
|
||||
int uwsgi_file_monitor_new() {
|
||||
|
||||
int inotify_fd ;
|
||||
|
||||
|
||||
inotify_fd = inotify_init();
|
||||
|
||||
if (inotify_fd < 0) {
|
||||
uwsgi_error("inotify_init()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return inotify_fd ;
|
||||
}
|
||||
|
||||
int uwsgi_file_monitor_add(int fd, char *what) {
|
||||
|
||||
int inotify_watch_fd ;
|
||||
|
||||
inotify_watch_fd = inotify_add_watch(fd, what, IN_ALL_EVENTS);
|
||||
|
||||
if (inotify_watch_fd < 0) {
|
||||
uwsgi_error("inotify_add_watch()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return inotify_watch_fd;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
import uwsgi
|
||||
import time
|
||||
import rrdtool
|
||||
|
||||
# rrdtool create test.rrd --start 1270879004 -s 10 DS:requests:COUNTER:300:U:U RRA:AVERAGE:0.5:3:105120
|
||||
|
||||
s_freq = 10
|
||||
|
||||
# this is a cron emulator done with the Spooler :)
|
||||
def rrdtool_updater(env):
|
||||
uwsgi.set_spooler_frequency(s_freq)
|
||||
rrdtool.update('../test.rrd', str(int(time.time()))+':'+str(uwsgi.total_requests()))
|
||||
uwsgi.send_to_spooler({})
|
||||
|
||||
|
||||
uwsgi.spooler = rrdtool_updater
|
||||
|
||||
|
||||
|
||||
def hello_world(env, start_response):
|
||||
start_response('200 Ok', [('Content-type', 'text/plain')])
|
||||
yield 'Hello world !'
|
||||
|
||||
|
||||
def graph(env, start_response):
|
||||
start_response('200 Ok', [('Content-type', 'image/png')])
|
||||
now = int(time.time())
|
||||
graph_range = (3600*24)
|
||||
rrdtool.graph('uwsgi_graph.png', '--start', str(now - graph_range), '--end', str(now), 'DEF:urequests=test.rrd:requests:AVERAGE', 'LINE2:urequests#00FF00')
|
||||
fd = open('uwsgi_graph.png', 'r')
|
||||
# send file to browser
|
||||
return env['wsgi.file_wrapper'](fd, 4096)
|
||||
|
||||
# start the simil-cron
|
||||
uwsgi.send_to_spooler({})
|
||||
|
||||
|
||||
uwsgi.applications = {'/': hello_world, '/graph':graph}
|
||||
@@ -0,0 +1,342 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
struct uwsgi_server uwsgi;
|
||||
|
||||
struct uwsgi_http_req {
|
||||
|
||||
pthread_t a_new_thread;
|
||||
int fd;
|
||||
struct sockaddr_in c_addr;
|
||||
socklen_t c_len;
|
||||
};
|
||||
|
||||
enum {
|
||||
uwsgi_http_method,
|
||||
uwsgi_http_uri,
|
||||
uwsgi_http_protocol,
|
||||
uwsgi_http_protocol_r,
|
||||
|
||||
uwsgi_http_header_key,
|
||||
uwsgi_http_header_key_colon,
|
||||
|
||||
uwsgi_http_header_val,
|
||||
uwsgi_http_header_val_r,
|
||||
|
||||
uwsgi_http_end
|
||||
};
|
||||
|
||||
void http_end() {
|
||||
uwsgi_log("closing uWSGI embedded HTTP server.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static char *add_uwsgi_var(char *up, char *key, uint16_t keylen, char *val, uint16_t vallen, int header, char *watermark)
|
||||
{
|
||||
|
||||
int i;
|
||||
|
||||
if (!header) {
|
||||
|
||||
if ( (up + 2 + keylen + 2 + vallen) > watermark ) return up ;
|
||||
|
||||
*up++ = (unsigned char) (keylen & 0xff);
|
||||
*up++ = (unsigned char) ((keylen >> 8) & 0xff);
|
||||
|
||||
memcpy(up, key, keylen);
|
||||
up += keylen;
|
||||
} else {
|
||||
|
||||
|
||||
for (i = 0; i < keylen; i++) {
|
||||
if (key[i] == '-') {
|
||||
key[i] = '_';
|
||||
} else {
|
||||
key[i] = toupper(key[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (strncmp("CONTENT_TYPE", key, keylen) && strncmp("CONTENT_LENGTH", key, keylen)) {
|
||||
if ( (up + 2 + keylen + 5 + 2 + vallen) > watermark ) return up ;
|
||||
*up++ = (unsigned char) (((uint16_t) keylen + 5) & 0xff);
|
||||
*up++ = (unsigned char) ((((uint16_t) keylen + 5) >> 8) & 0xff);
|
||||
memcpy(up, "HTTP_", 5);
|
||||
up += 5;
|
||||
} else {
|
||||
if ( (up + 2 + keylen + 2 + vallen) > watermark ) return up ;
|
||||
*up++ = (unsigned char) (keylen & 0xff);
|
||||
*up++ = (unsigned char) ((keylen >> 8) & 0xff);
|
||||
}
|
||||
|
||||
|
||||
memcpy(up, key, keylen);
|
||||
up += keylen;
|
||||
}
|
||||
|
||||
*up++ = (unsigned char) (vallen & 0xff);
|
||||
*up++ = (unsigned char) ((vallen >> 8) & 0xff);
|
||||
memcpy(up, val, vallen);
|
||||
up += vallen;
|
||||
|
||||
return up;
|
||||
}
|
||||
|
||||
static void *http_request(void *);
|
||||
|
||||
void http_loop(struct uwsgi_server * uwsgi)
|
||||
{
|
||||
|
||||
|
||||
struct uwsgi_http_req *ur;
|
||||
int ret;
|
||||
pthread_attr_t pa;
|
||||
int stat_loc ;
|
||||
|
||||
ret = pthread_attr_init(&pa);
|
||||
if (ret) {
|
||||
uwsgi_log("pthread_attr_init() = %d\n", ret);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ret = pthread_attr_setdetachstate(&pa, PTHREAD_CREATE_DETACHED);
|
||||
if (ret) {
|
||||
uwsgi_log("pthread_attr_setdetachstate() = %d\n", ret);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!uwsgi->http_only) {
|
||||
signal(SIGCHLD, &http_end);
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
|
||||
if (!uwsgi->http_only) {
|
||||
if (waitpid(-1, &stat_loc, WNOHANG) != 0) {
|
||||
http_end();
|
||||
}
|
||||
}
|
||||
ur = malloc(sizeof(struct uwsgi_http_req));
|
||||
if (!ur) {
|
||||
uwsgi_error("malloc()");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
ur->c_len = sizeof(struct sockaddr_in) ;
|
||||
ur->fd = accept(uwsgi->http_fd, (struct sockaddr *) &ur->c_addr, &ur->c_len);
|
||||
|
||||
if (ur->fd < 0) {
|
||||
uwsgi_error("accept()");
|
||||
free(ur);
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = pthread_create(&ur->a_new_thread, &pa, http_request, (void *) ur);
|
||||
if (ret) {
|
||||
uwsgi_log("pthread_create() = %d\n", ret);
|
||||
free(ur);
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void *http_request(void *u_h_r)
|
||||
{
|
||||
|
||||
char buf[4096];
|
||||
|
||||
char tmp_buf[4096];
|
||||
char *ptr;
|
||||
|
||||
char uwsgipkt[4096];
|
||||
char *up;
|
||||
|
||||
struct uwsgi_http_req *ur = (struct uwsgi_http_req *) u_h_r ;
|
||||
|
||||
int clientfd = ur->fd;
|
||||
int uwsgi_fd;
|
||||
|
||||
int need_to_read = 1;
|
||||
int state = uwsgi_http_method;
|
||||
|
||||
int http_body_len = 0;
|
||||
|
||||
size_t len;
|
||||
|
||||
int i,
|
||||
j;
|
||||
|
||||
char HTTP_header_key[1024];
|
||||
|
||||
uint16_t ulen;
|
||||
|
||||
ptr = tmp_buf;
|
||||
|
||||
int qs = 0;
|
||||
|
||||
up = uwsgipkt;
|
||||
|
||||
char *watermark = up + 4096 ;
|
||||
|
||||
up[0] = 0;
|
||||
up[3] = 0;
|
||||
up += 4;
|
||||
|
||||
while (need_to_read) {
|
||||
len = read(clientfd, buf, 4096);
|
||||
if (len <= 0) {
|
||||
uwsgi_error("read()");
|
||||
break;
|
||||
}
|
||||
for (i = 0; i < len; i++) {
|
||||
|
||||
if (buf[i] == ' ') {
|
||||
|
||||
if (state == uwsgi_http_method) {
|
||||
|
||||
up = add_uwsgi_var(up, "REQUEST_METHOD", 14, tmp_buf, ptr - tmp_buf, 0, watermark);
|
||||
ptr = tmp_buf;
|
||||
state = uwsgi_http_uri;
|
||||
|
||||
} else if (state == uwsgi_http_uri) {
|
||||
|
||||
up = add_uwsgi_var(up, "REQUEST_URI", 11, tmp_buf, ptr - tmp_buf, 0, watermark);
|
||||
|
||||
int path_info_len = ptr - tmp_buf;
|
||||
for (j = 0; j < ptr - tmp_buf; j++) {
|
||||
if (tmp_buf[j] == '?') {
|
||||
path_info_len = j;
|
||||
if (j + 1 < (ptr - tmp_buf)) {
|
||||
up = add_uwsgi_var(up, "QUERY_STRING", 12, tmp_buf + j + 1, (ptr - tmp_buf) - (j + 1), 0, watermark);
|
||||
qs = 1 ;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!qs) {
|
||||
up = add_uwsgi_var(up, "QUERY_STRING", 12, NULL, 0, 0, watermark);
|
||||
}
|
||||
up = add_uwsgi_var(up, "PATH_INFO", 9, tmp_buf, path_info_len, 0, watermark);
|
||||
|
||||
ptr = tmp_buf;
|
||||
state = uwsgi_http_protocol;
|
||||
|
||||
} else if (state == uwsgi_http_header_key_colon) {
|
||||
|
||||
*ptr++ = 0;
|
||||
|
||||
memset(HTTP_header_key, 0, sizeof(HTTP_header_key));
|
||||
memcpy(HTTP_header_key, tmp_buf, strlen(tmp_buf));
|
||||
ptr = tmp_buf;
|
||||
state = uwsgi_http_header_val;
|
||||
} else {
|
||||
//check for overflow
|
||||
*ptr++ = buf[i];
|
||||
}
|
||||
|
||||
} else if (buf[i] == '\r') {
|
||||
|
||||
if (state == uwsgi_http_protocol) {
|
||||
state = uwsgi_http_protocol_r;
|
||||
}
|
||||
if (state == uwsgi_http_header_val) {
|
||||
state = uwsgi_http_header_val_r;
|
||||
} else if (state == uwsgi_http_header_key) {
|
||||
state = uwsgi_http_end;
|
||||
}
|
||||
} else if (buf[i] == '\n') {
|
||||
|
||||
if (state == uwsgi_http_header_val_r) {
|
||||
|
||||
up = add_uwsgi_var(up, HTTP_header_key, strlen(HTTP_header_key), tmp_buf, ptr - tmp_buf, 1, watermark);
|
||||
if (!strcmp("CONTENT_LENGTH", HTTP_header_key)) {
|
||||
*ptr++ = 0;
|
||||
http_body_len = atoi(tmp_buf);
|
||||
}
|
||||
ptr = tmp_buf;
|
||||
state = uwsgi_http_header_key;
|
||||
} else if (state == uwsgi_http_protocol_r) {
|
||||
|
||||
up = add_uwsgi_var(up, "SERVER_PROTOCOL", 15, tmp_buf, ptr - tmp_buf, 0, watermark);
|
||||
ptr = tmp_buf;
|
||||
state = uwsgi_http_header_key;
|
||||
} else if (state == uwsgi_http_end) {
|
||||
need_to_read = 0;
|
||||
|
||||
|
||||
if (uwsgi.http_server_name) {
|
||||
up = add_uwsgi_var(up, "SERVER_NAME", 11, uwsgi.http_server_name, strlen(uwsgi.http_server_name), 0, watermark);
|
||||
} else {
|
||||
up = add_uwsgi_var(up, "SERVER_NAME", 11, "localhost", 9, 0, watermark);
|
||||
}
|
||||
|
||||
up = add_uwsgi_var(up, "SERVER_PORT", 11, uwsgi.http_server_port, strlen(uwsgi.http_server_port), 0, watermark);
|
||||
up = add_uwsgi_var(up, "SCRIPT_NAME", 11, "", 0, 0, watermark);
|
||||
|
||||
char *ip = inet_ntoa(ur->c_addr.sin_addr);
|
||||
up = add_uwsgi_var(up, "REMOTE_ADDR", 11, ip, strlen(ip), 0, watermark);
|
||||
|
||||
//up = add_uwsgi_var(up, "REMOTE_USER", 11, "unknown", 7, 0);
|
||||
|
||||
uwsgi_fd = uwsgi_connect(uwsgi.socket_name, 10);
|
||||
if (uwsgi_fd >= 0) {
|
||||
ulen = (up - uwsgipkt) - 4;
|
||||
uwsgipkt[1] = (unsigned char) (ulen & 0xff);
|
||||
uwsgipkt[2] = (unsigned char) ((ulen >> 8) & 0xff);
|
||||
|
||||
write(uwsgi_fd, uwsgipkt, ulen + 4);
|
||||
|
||||
if (http_body_len > 0) {
|
||||
if (http_body_len >= len - (i + 1)) {
|
||||
write(uwsgi_fd, buf + i + 1, len - (i + 1));
|
||||
http_body_len -= len - (i + 1);
|
||||
} else {
|
||||
write(uwsgi_fd, buf + i, http_body_len);
|
||||
http_body_len = 0;
|
||||
}
|
||||
|
||||
while (http_body_len > 0) {
|
||||
int to_read = 4096;
|
||||
if (http_body_len < to_read) {
|
||||
to_read = http_body_len;
|
||||
}
|
||||
len = read(clientfd, uwsgipkt, to_read);
|
||||
write(uwsgi_fd, uwsgipkt, len);
|
||||
http_body_len -= len;
|
||||
}
|
||||
}
|
||||
while ((len = read(uwsgi_fd, uwsgipkt, 4096)) > 0) {
|
||||
write(clientfd, uwsgipkt, len);
|
||||
}
|
||||
close(uwsgi_fd);
|
||||
}
|
||||
else {
|
||||
close(uwsgi_fd);
|
||||
goto clear;
|
||||
}
|
||||
}
|
||||
} else if (buf[i] == ':') {
|
||||
|
||||
if (state == uwsgi_http_header_key) {
|
||||
state = uwsgi_http_header_key_colon;
|
||||
} else {
|
||||
//check for overflow
|
||||
*ptr++ = buf[i];
|
||||
}
|
||||
} else {
|
||||
|
||||
//check for overflow
|
||||
*ptr++ = buf[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
clear:
|
||||
close(clientfd);
|
||||
|
||||
free(ur);
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
#ifdef UWSGI_INI
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
/*
|
||||
ini file must be read ALL into memory.
|
||||
This memory must not be freed for all the server lifecycle
|
||||
*/
|
||||
|
||||
enum {
|
||||
ini_key_start,
|
||||
ini_val_start,
|
||||
};
|
||||
|
||||
void ini_rstrip(char *line) {
|
||||
|
||||
off_t i ;
|
||||
|
||||
for(i = strlen(line)-1;i>=0; i--) {
|
||||
if (line[i] == ' ' || line[i] == '\t') {
|
||||
line[i] = 0;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char *ini_lstrip(char *line) {
|
||||
|
||||
off_t i ;
|
||||
char *ptr = line;
|
||||
|
||||
for(i=0;i<strlen(line);i++) {
|
||||
if (line[i] == ' ' || line[i] == '\t') {
|
||||
ptr++;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char *ini_get_key(char *key) {
|
||||
|
||||
off_t i;
|
||||
char *ptr = key ;
|
||||
|
||||
for(i=0;i<strlen(key);i++) {
|
||||
ptr++ ;
|
||||
if (key[i] == '=') {
|
||||
key[i] = 0;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char *ini_get_line(char *ini, off_t size) {
|
||||
|
||||
off_t i ;
|
||||
char *ptr = ini;
|
||||
|
||||
for(i=0;i<size;i++) {
|
||||
ptr++;
|
||||
if (ini[i] == '\n') {
|
||||
ini[i] = 0;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_ini_config(char *file, struct option *long_options) {
|
||||
|
||||
int fd;
|
||||
ssize_t len;
|
||||
char *ini;
|
||||
struct stat sb;
|
||||
|
||||
char *ini_line;
|
||||
|
||||
char *section;
|
||||
char *key;
|
||||
char *val;
|
||||
|
||||
int lines = 1 ;
|
||||
|
||||
struct option *lopt, *aopt;
|
||||
char *section_asked = "uwsgi";
|
||||
char *colon ;
|
||||
|
||||
colon = strchr(file, ':');
|
||||
if (colon) {
|
||||
colon[0] = 0;
|
||||
section_asked = colon+1 ;
|
||||
}
|
||||
|
||||
fd = open(file, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (fstat(fd, &sb)) {
|
||||
uwsgi_error("fstat()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ini = malloc(sb.st_size+1);
|
||||
|
||||
if (!ini) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
len = read(fd, ini, sb.st_size);
|
||||
if (len != sb.st_size) {
|
||||
uwsgi_error("read()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ini[sb.st_size+1] = 0 ;
|
||||
|
||||
while(sb.st_size) {
|
||||
ini_line = ini_get_line(ini, sb.st_size);
|
||||
if (ini_line == NULL) {
|
||||
break;
|
||||
}
|
||||
lines++;
|
||||
|
||||
// skip empty line
|
||||
key = ini_lstrip(ini);
|
||||
ini_rstrip(key);
|
||||
if (key[0] != 0) {
|
||||
if (key[0] == '[') {
|
||||
section = key+1;
|
||||
section[strlen(section)-1] = 0;
|
||||
}
|
||||
else if (key[0] == ';' || key[0] == '#') {
|
||||
// this is a comment
|
||||
}
|
||||
else {
|
||||
// val is always valid, but (obviously can be ignored)
|
||||
val = ini_get_key(key);
|
||||
|
||||
if (!strcmp(section, section_asked)) {
|
||||
ini_rstrip(key);
|
||||
val = ini_lstrip(val);
|
||||
ini_rstrip(val);
|
||||
lopt = long_options;
|
||||
while ((aopt = lopt)) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
if (!strcmp(key, aopt->name)) {
|
||||
if (aopt->flag) {
|
||||
*aopt->flag = aopt->val;
|
||||
}
|
||||
else {
|
||||
manage_opt(aopt->val, val);
|
||||
}
|
||||
}
|
||||
lopt++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ini += (ini_line - ini);
|
||||
sb.st_size -= (ini_line - ini);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,352 @@
|
||||
#ifdef UWSGI_LDAP
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
#include <ldap.h>
|
||||
|
||||
void ldap2uwsgi(char *ldapname, char *uwsginame) {
|
||||
|
||||
char *ptr = uwsginame ;
|
||||
|
||||
int i;
|
||||
|
||||
for(i=0;i<strlen(ldapname);i++) {
|
||||
if (isupper(ldapname[i])) {
|
||||
*ptr++= '-';
|
||||
*ptr++= tolower(ldapname[i]);
|
||||
}
|
||||
else {
|
||||
*ptr++= ldapname[i] ;
|
||||
}
|
||||
}
|
||||
|
||||
*ptr++= 0;
|
||||
}
|
||||
|
||||
int calc_ldap_name(char *name) {
|
||||
|
||||
int i;
|
||||
int counter = 0;
|
||||
|
||||
for(i=0;i<strlen(name);i++) {
|
||||
if (isupper(name[i])) {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
return strlen(name) + counter ;
|
||||
}
|
||||
|
||||
void uwsgi_ldap_schema_dump_ldif(struct option *lo) {
|
||||
|
||||
struct option *aopt, *lopt ;
|
||||
|
||||
lopt = lo;
|
||||
int i;
|
||||
int counter = 30000;
|
||||
|
||||
int pythonpath_found = 0 ;
|
||||
|
||||
uwsgi_log("\n");
|
||||
uwsgi_log("dn: cn=uwsgi,cn=schema,cn=config\n");
|
||||
uwsgi_log("objectClass: olcSchemaConfig\n");
|
||||
uwsgi_log("cn: uwsgi\n");
|
||||
|
||||
while( (aopt = lopt) ) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
|
||||
if (aopt->val == LONG_ARGS_PYTHONPATH) {
|
||||
if (pythonpath_found) {
|
||||
goto next;
|
||||
}
|
||||
pythonpath_found = 1;
|
||||
}
|
||||
|
||||
if (aopt->flag) {
|
||||
uwsgi_log("olcAttributeTypes: ( 1.3.6.1.4.1.35156.17.4.%d NAME 'uWSGI", counter) ;
|
||||
counter++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("olcAttributeTypes: ( 1.3.6.1.4.1.35156.17.4.%d NAME 'uWSGI", aopt->val) ;
|
||||
}
|
||||
for(i=0;i<strlen(aopt->name);i++) {
|
||||
if (aopt->name[i] == '-') {
|
||||
i++;
|
||||
uwsgi_log("%c", toupper(aopt->name[i]));
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%c", aopt->name[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (aopt->has_arg) {
|
||||
uwsgi_log("' SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("' SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )\n");
|
||||
}
|
||||
next:
|
||||
lopt++;
|
||||
}
|
||||
|
||||
uwsgi_log("olcAttributeTypes: ( 1.3.6.1.4.1.35156.17.4.50000 NAME 'uWSGInull' SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )\n") ;
|
||||
|
||||
lopt = lo;
|
||||
|
||||
uwsgi_log("olcObjectClasses: ( 1.3.6.1.4.1.35156.17.3.1 NAME 'uWSGIConfig' SUP top AUXILIARY DESC 'uWSGI configuration' MAY ( ");
|
||||
|
||||
pythonpath_found = 0;
|
||||
while( (aopt = lopt) ) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
|
||||
if (aopt->val == LONG_ARGS_PYTHONPATH) {
|
||||
if (pythonpath_found) {
|
||||
goto next2;
|
||||
}
|
||||
pythonpath_found = 1;
|
||||
}
|
||||
|
||||
uwsgi_log("uWSGI");
|
||||
|
||||
for(i=0;i<strlen(aopt->name);i++) {
|
||||
if (aopt->name[i] == '-') {
|
||||
i++;
|
||||
uwsgi_log("%c", toupper(aopt->name[i]));
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%c", aopt->name[i]);
|
||||
}
|
||||
}
|
||||
uwsgi_log(" $ ");
|
||||
next2:
|
||||
lopt++;
|
||||
}
|
||||
|
||||
uwsgi_log("uWSGInull ))\n");
|
||||
|
||||
uwsgi_log("\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_ldap_schema_dump(struct option *lo) {
|
||||
|
||||
struct option *aopt, *lopt ;
|
||||
|
||||
lopt = lo;
|
||||
int i;
|
||||
int counter = 30000;
|
||||
int pythonpath_found = 0;
|
||||
|
||||
while( (aopt = lopt) ) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
|
||||
|
||||
if (aopt->val == LONG_ARGS_PYTHONPATH) {
|
||||
if (pythonpath_found) {
|
||||
goto next;
|
||||
}
|
||||
pythonpath_found = 1;
|
||||
}
|
||||
|
||||
if (aopt->flag) {
|
||||
uwsgi_log("attributetype ( 1.3.6.1.4.1.35156.17.4.%d NAME 'uWSGI", counter) ;
|
||||
counter++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log("attributetype ( 1.3.6.1.4.1.35156.17.4.%d NAME 'uWSGI", aopt->val) ;
|
||||
}
|
||||
for(i=0;i<strlen(aopt->name);i++) {
|
||||
if (aopt->name[i] == '-') {
|
||||
i++;
|
||||
uwsgi_log("%c", toupper(aopt->name[i]));
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%c", aopt->name[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (aopt->has_arg) {
|
||||
uwsgi_log("' SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )\n");
|
||||
}
|
||||
else {
|
||||
uwsgi_log("' SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )\n");
|
||||
}
|
||||
next:
|
||||
lopt++;
|
||||
}
|
||||
|
||||
uwsgi_log("attributetype ( 1.3.6.1.4.1.35156.17.4.50000 NAME 'uWSGInull' SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 )\n") ;
|
||||
|
||||
lopt = lo;
|
||||
|
||||
uwsgi_log("objectclass ( 1.3.6.1.4.1.35156.17.3.1 NAME 'uWSGIConfig' SUP top AUXILIARY DESC 'uWSGI configuration' MAY ( ");
|
||||
|
||||
pythonpath_found = 0;
|
||||
while( (aopt = lopt) ) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
|
||||
if (aopt->val == LONG_ARGS_PYTHONPATH) {
|
||||
if (pythonpath_found) {
|
||||
goto next2;
|
||||
}
|
||||
pythonpath_found = 1;
|
||||
}
|
||||
|
||||
uwsgi_log("uWSGI");
|
||||
|
||||
for(i=0;i<strlen(aopt->name);i++) {
|
||||
if (aopt->name[i] == '-') {
|
||||
i++;
|
||||
uwsgi_log("%c", toupper(aopt->name[i]));
|
||||
}
|
||||
else {
|
||||
uwsgi_log("%c", aopt->name[i]);
|
||||
}
|
||||
}
|
||||
uwsgi_log(" $ ");
|
||||
next2:
|
||||
lopt++;
|
||||
}
|
||||
|
||||
uwsgi_log("uWSGInull ))\n");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void uwsgi_ldap_config(struct uwsgi_server *uwsgi, struct option *long_options) {
|
||||
|
||||
LDAP *ldp;
|
||||
LDAPMessage *results, *entry;
|
||||
BerElement *ber;
|
||||
struct berval **bervalues;
|
||||
char *attr;
|
||||
char *uwsgi_attr;
|
||||
|
||||
char *url = "ldap:///";
|
||||
char *url_slash;
|
||||
|
||||
int desired_version = LDAP_VERSION3;
|
||||
int ret;
|
||||
|
||||
struct option *aopt, *lopt;
|
||||
|
||||
LDAPURLDesc *ldap_url;
|
||||
|
||||
if (uwsgi->ldap) {
|
||||
url = uwsgi->ldap;
|
||||
}
|
||||
|
||||
if (!ldap_is_ldap_url(url)) {
|
||||
uwsgi_log("invalid LDAP url.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (ldap_url_parse(url, &ldap_url) != LDAP_SUCCESS) {
|
||||
uwsgi_log("unable to parse LDAP url.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
url_slash = strchr(url, '/');
|
||||
url_slash = strchr(url_slash + 1, '/');
|
||||
|
||||
url_slash = strchr(url_slash + 1, '/');
|
||||
if (url_slash) {
|
||||
url_slash[0] = 0;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("LDAP URL: %s\n", url);
|
||||
uwsgi_debug("LDAP BASE DN: %s\n", ldap_url->lud_dn);
|
||||
#endif
|
||||
|
||||
if ( (ret = ldap_initialize( &ldp, url)) != LDAP_SUCCESS) {
|
||||
uwsgi_log("LDAP: %s\n", ldap_err2string(ret));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
if ( (ret = ldap_set_option(ldp, LDAP_OPT_PROTOCOL_VERSION, &desired_version)) != LDAP_OPT_SUCCESS) {
|
||||
uwsgi_log("LDAP: %s\n", ldap_err2string(ret));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
if ( (ret = ldap_search_ext_s(ldp, ldap_url->lud_dn, ldap_url->lud_scope, ldap_url->lud_filter, NULL, 0, NULL, NULL, NULL, 1, &results)) != LDAP_SUCCESS) {
|
||||
uwsgi_log("LDAP: %s\n", ldap_err2string(ret));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("LDAP connection initialized %p\n", ldp);
|
||||
#endif
|
||||
|
||||
free(ldap_url);
|
||||
|
||||
if (ldap_count_entries(ldp, results) < 1) {
|
||||
uwsgi_log("no LDAP entry found\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
entry = ldap_first_entry(ldp, results);
|
||||
|
||||
for( attr = ldap_first_attribute(ldp, entry, &ber); attr != NULL; attr = ldap_next_attribute(ldp, entry, ber)) {
|
||||
if (!strncmp(attr,"uWSGI",5)) {
|
||||
|
||||
uwsgi_attr = malloc( calc_ldap_name(attr) + 1 );
|
||||
if (!uwsgi_attr) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ldap2uwsgi(attr+5, uwsgi_attr);
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("LDAP attribute: %s = --%s\n", attr, uwsgi_attr);
|
||||
#endif
|
||||
bervalues = ldap_get_values_len(ldp, entry, attr);
|
||||
if (bervalues) {
|
||||
// do not free uwsgi_val;
|
||||
char *uwsgi_val = malloc( bervalues[0]->bv_len + 1 );
|
||||
if (!uwsgi_val) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(uwsgi_val, bervalues[0]->bv_val, bervalues[0]->bv_len);
|
||||
uwsgi_val[bervalues[0]->bv_len] = 0;
|
||||
|
||||
lopt = long_options;
|
||||
while ((aopt = lopt)) {
|
||||
if (!aopt->name)
|
||||
break;
|
||||
if (!strcmp(uwsgi_attr, aopt->name)) {
|
||||
if (aopt->flag) {
|
||||
*aopt->flag = aopt->val;
|
||||
}
|
||||
else {
|
||||
manage_opt(aopt->val, uwsgi_val);
|
||||
}
|
||||
}
|
||||
lopt++;
|
||||
}
|
||||
}
|
||||
|
||||
free(bervalues);
|
||||
free(uwsgi_attr);
|
||||
}
|
||||
free(attr);
|
||||
}
|
||||
|
||||
free(ber);
|
||||
free(results);
|
||||
|
||||
ldap_unbind_ext_s(ldp, NULL, NULL);
|
||||
|
||||
}
|
||||
#endif
|
||||
+110
-64
@@ -35,6 +35,23 @@
|
||||
#define data_uwsgi data_fastcgi
|
||||
#define data_uwsgi_init data_fastcgi_init
|
||||
|
||||
#ifdef __linux__
|
||||
#include <endian.h>
|
||||
#elif __sun__
|
||||
#elif __APPLE__
|
||||
#include <libkern/OSByteOrder.h>
|
||||
#else
|
||||
#include <machine/endian.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
static uint16_t uwsgi_swap16(uint16_t x) {
|
||||
return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* the uwsgi module is based on the proxy module
|
||||
@@ -49,23 +66,15 @@ typedef struct __attribute__((packed)) {
|
||||
uint8_t modifier2;
|
||||
} uwsgi_header ;
|
||||
|
||||
typedef enum {
|
||||
UWSGI_BALANCE_UNSET,
|
||||
UWSGI_BALANCE_FAIR,
|
||||
} uwsgi_balance_t;
|
||||
|
||||
typedef struct {
|
||||
array *extensions;
|
||||
unsigned short debug;
|
||||
|
||||
uwsgi_balance_t balance;
|
||||
} plugin_config;
|
||||
|
||||
typedef struct {
|
||||
PLUGIN_DATA;
|
||||
|
||||
buffer *parse_response;
|
||||
buffer *balance_buf;
|
||||
|
||||
plugin_config **config_storage;
|
||||
|
||||
@@ -121,6 +130,7 @@ static handler_ctx * handler_ctx_init() {
|
||||
hctx->fd = -1;
|
||||
hctx->fde_ndx = -1;
|
||||
|
||||
|
||||
return hctx;
|
||||
}
|
||||
|
||||
@@ -138,7 +148,6 @@ INIT_FUNC(mod_uwsgi_init) {
|
||||
p = calloc(1, sizeof(*p));
|
||||
|
||||
p->parse_response = buffer_init();
|
||||
p->balance_buf = buffer_init();
|
||||
|
||||
return p;
|
||||
}
|
||||
@@ -150,7 +159,6 @@ FREE_FUNC(mod_uwsgi_free) {
|
||||
UNUSED(srv);
|
||||
|
||||
buffer_free(p->parse_response);
|
||||
buffer_free(p->balance_buf);
|
||||
|
||||
if (p->config_storage) {
|
||||
size_t i;
|
||||
@@ -180,7 +188,6 @@ SETDEFAULTS_FUNC(mod_uwsgi_set_defaults) {
|
||||
config_values_t cv[] = {
|
||||
{ "uwsgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
|
||||
{ "uwsgi.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
|
||||
{ "uwsgi.balance", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
|
||||
{ NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
|
||||
};
|
||||
|
||||
@@ -196,9 +203,7 @@ SETDEFAULTS_FUNC(mod_uwsgi_set_defaults) {
|
||||
|
||||
cv[0].destination = s->extensions;
|
||||
cv[1].destination = &(s->debug);
|
||||
cv[2].destination = p->balance_buf;
|
||||
|
||||
buffer_reset(p->balance_buf);
|
||||
|
||||
p->config_storage[i] = s;
|
||||
ca = ((data_config *)srv->config_context->data[i])->value;
|
||||
@@ -207,16 +212,6 @@ SETDEFAULTS_FUNC(mod_uwsgi_set_defaults) {
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
|
||||
if (buffer_is_empty(p->balance_buf)) {
|
||||
s->balance = UWSGI_BALANCE_FAIR;
|
||||
} else if (buffer_is_equal_string(p->balance_buf, CONST_STR_LEN("fair"))) {
|
||||
s->balance = UWSGI_BALANCE_FAIR;
|
||||
} else {
|
||||
log_error_write(srv, __FILE__, __LINE__, "s",
|
||||
"uwsgi.balance has to be fair");
|
||||
return HANDLER_ERROR;
|
||||
}
|
||||
|
||||
if (NULL != (du = array_get_element(ca, "uwsgi.server"))) {
|
||||
size_t j;
|
||||
data_array *da = (data_array *)du;
|
||||
@@ -276,7 +271,7 @@ SETDEFAULTS_FUNC(mod_uwsgi_set_defaults) {
|
||||
|
||||
df = data_uwsgi_init();
|
||||
|
||||
df->port = 80;
|
||||
df->port = 3031;
|
||||
|
||||
buffer_copy_string_buffer(df->key, da_host->key);
|
||||
|
||||
@@ -396,12 +391,28 @@ static int uwsgi_establish_connection(server *srv, handler_ctx *hctx) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void uwsgi_add_var(buffer *b, char * key, uint16_t keylen, char *val, uint16_t vallen) {
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uint16_t uwlen ;
|
||||
|
||||
uwlen = uwsgi_swap16(keylen);
|
||||
buffer_append_memory(b, (char *) &uwlen, 2); buffer_append_memory(b, key, keylen);
|
||||
uwlen = uwsgi_swap16(vallen);
|
||||
buffer_append_memory(b, (char *) &uwlen, 2); buffer_append_memory(b, val, vallen);
|
||||
#else
|
||||
buffer_append_memory(b, (char *) &keylen, 2); buffer_append_memory(b, key, keylen);
|
||||
buffer_append_memory(b, (char *) &vallen, 2); buffer_append_memory(b, val, vallen);
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
static int uwsgi_create_env(server *srv, handler_ctx *hctx) {
|
||||
size_t i;
|
||||
|
||||
char buf[32];
|
||||
uwsgi_header *uh ;
|
||||
uint16_t uwsgi_strsize;
|
||||
const char *s;
|
||||
#ifdef HAVE_IPV6
|
||||
char b2[INET6_ADDRSTRLEN + 1];
|
||||
@@ -416,36 +427,28 @@ static int uwsgi_create_env(server *srv, handler_ctx *hctx) {
|
||||
|
||||
b = chunkqueue_get_append_buffer(hctx->wb);
|
||||
|
||||
log_error_write(srv, __FILE__, __LINE__, "s", "starting uwsgi request...");
|
||||
|
||||
/* make space for uwsgi header */
|
||||
buffer_append_memory(b, "\0\0\0\0", 4);
|
||||
|
||||
log_error_write(srv, __FILE__, __LINE__, "s", "space done...");
|
||||
|
||||
/* set WSGI vars */
|
||||
s = get_http_method_name(con->request.http_method);
|
||||
uwsgi_strsize = 14; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "REQUEST_METHOD", uwsgi_strsize);
|
||||
uwsgi_strsize = strlen(s); buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
uwsgi_add_var(b, "REQUEST_METHOD", 14, (char *) s, strlen(s));
|
||||
|
||||
uwsgi_strsize = 12; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "QUERY_STRING", uwsgi_strsize);
|
||||
if (!buffer_is_empty(con->uri.query)) {
|
||||
uwsgi_strsize = con->uri.query->used; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, con->uri.query->ptr, uwsgi_strsize);
|
||||
uwsgi_add_var(b, "QUERY_STRING", 12, con->uri.query->ptr, con->uri.query->used-1);
|
||||
} else {
|
||||
uwsgi_strsize = 0; buffer_append_memory(b, &uwsgi_strsize, 2);
|
||||
uwsgi_add_var(b, "QUERY_STRING", 12, "", 0);
|
||||
}
|
||||
|
||||
s = get_http_version_name(con->request.http_version);
|
||||
uwsgi_strsize = 15; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "SERVER_PROTOCOL", uwsgi_strsize);
|
||||
uwsgi_strsize = strlen(s); buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
uwsgi_add_var(b, "SERVER_PROTOCOL", 15, (char *) s, strlen(s));
|
||||
|
||||
|
||||
uwsgi_strsize = 11; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "SERVER_NAME", uwsgi_strsize);
|
||||
if (con->server_name->used) {
|
||||
size_t len = con->server_name->used - 1;
|
||||
char *colon = strchr(con->server_name->ptr, ':');
|
||||
if (colon) len = colon - con->server_name->ptr;
|
||||
uwsgi_strsize = len ;
|
||||
uwsgi_add_var(b, "SERVER_NAME", 11, con->server_name->ptr, len);
|
||||
|
||||
} else {
|
||||
#ifdef HAVE_IPV6
|
||||
@@ -457,11 +460,9 @@ static int uwsgi_create_env(server *srv, handler_ctx *hctx) {
|
||||
#else
|
||||
s = inet_ntoa(srv_sock->addr.ipv4.sin_addr);
|
||||
#endif
|
||||
uwsgi_strsize = strlen(s);
|
||||
uwsgi_add_var(b, "SERVER_NAME", 11, (char *)s, strlen(s));
|
||||
}
|
||||
buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
|
||||
uwsgi_strsize = 11; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "SERVER_PORT", uwsgi_strsize);
|
||||
LI_ltostr(buf,
|
||||
#ifdef HAVE_IPV6
|
||||
ntohs(srv_sock->addr.plain.sa_family ? srv_sock->addr.ipv6.sin6_port : srv_sock->addr.ipv4.sin_port)
|
||||
@@ -470,12 +471,9 @@ static int uwsgi_create_env(server *srv, handler_ctx *hctx) {
|
||||
#endif
|
||||
);
|
||||
|
||||
uwsgi_strsize = strlen(s); buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
uwsgi_add_var(b, "SERVER_PORT", 11, buf, strlen(buf));
|
||||
uwsgi_add_var(b, "REQUEST_URI", 11, con->request.orig_uri->ptr, con->request.orig_uri->used-1);
|
||||
|
||||
uwsgi_strsize = 11; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "REQUEST_URI", uwsgi_strsize);
|
||||
uwsgi_strsize = con->request.orig_uri->used; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, con->request.orig_uri->ptr, uwsgi_strsize);
|
||||
|
||||
uwsgi_strsize = 11; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "REMOTE_PORT", uwsgi_strsize);
|
||||
LI_ltostr(buf,
|
||||
#ifdef HAVE_IPV6
|
||||
ntohs(con->dst_addr.plain.sa_family ? con->dst_addr.ipv6.sin6_port : con->dst_addr.ipv4.sin_port)
|
||||
@@ -483,23 +481,81 @@ static int uwsgi_create_env(server *srv, handler_ctx *hctx) {
|
||||
ntohs(con->dst_addr.ipv4.sin_port)
|
||||
#endif
|
||||
);
|
||||
uwsgi_add_var(b, "REMOTE_PORT", 11, buf, strlen(buf));
|
||||
|
||||
if (!buffer_is_empty(con->authed_user)) {
|
||||
uwsgi_add_var(b, "REMOTE_USER", 11,
|
||||
CONST_BUF_LEN(con->authed_user));
|
||||
}
|
||||
|
||||
|
||||
uwsgi_strsize = strlen(s); buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
|
||||
uwsgi_strsize = 11; buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, "REMOTE_ADDR", uwsgi_strsize);
|
||||
s = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
|
||||
uwsgi_strsize = strlen(s); buffer_append_memory(b, &uwsgi_strsize, 2); buffer_append_memory(b, s, uwsgi_strsize);
|
||||
|
||||
uwsgi_add_var(b, "REMOTE_ADDR", 11, (char *)s, strlen(s));
|
||||
|
||||
if (hctx->path_info_offset > 1) {
|
||||
uwsgi_add_var(b, "SCRIPT_NAME", 11, con->uri.path->ptr, hctx->path_info_offset);
|
||||
if (strlen(con->uri.path->ptr + hctx->path_info_offset) > 0) {
|
||||
uwsgi_add_var(b, "PATH_INFO", 9, con->uri.path->ptr + hctx->path_info_offset, strlen(con->uri.path->ptr + hctx->path_info_offset));
|
||||
}
|
||||
else {
|
||||
uwsgi_add_var(b, "PATH_INFO", 9, "/", 1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_add_var(b, "SCRIPT_NAME", 11, "", 0) ;
|
||||
uwsgi_add_var(b, "PATH_INFO", 9, con->uri.path->ptr, strlen(con->uri.path->ptr)) ;
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < con->request.headers->used; i++) {
|
||||
data_string *ds;
|
||||
ds = (data_string *)con->request.headers->data[i];
|
||||
|
||||
if (ds->value->used && ds->key->used) {
|
||||
size_t j;
|
||||
buffer_reset(srv->tmp_buf);
|
||||
if (!strcasecmp(ds->key->ptr, "CONTENT-TYPE") || !strcasecmp(ds->key->ptr, "CONTENT-LENGTH")) {
|
||||
buffer_prepare_append(srv->tmp_buf, ds->key->used + 2);
|
||||
}
|
||||
else {
|
||||
buffer_prepare_append(srv->tmp_buf, ds->key->used + 7);
|
||||
buffer_append_memory(srv->tmp_buf, "HTTP_", 5);
|
||||
}
|
||||
for (j = 0; j < ds->key->used - 1; j++) {
|
||||
srv->tmp_buf->ptr[srv->tmp_buf->used++] = light_isalpha(ds->key->ptr[j]) ? ds->key->ptr[j] & ~32 : '_';
|
||||
}
|
||||
srv->tmp_buf->ptr[srv->tmp_buf->used++] = '\0';
|
||||
uwsgi_add_var(b, srv->tmp_buf->ptr, srv->tmp_buf->used-1, ds->value->ptr, ds->value->used-1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < con->environment->used; i++) {
|
||||
data_string *ds;
|
||||
ds = (data_string *)con->environment->data[i];
|
||||
if (ds->value->used && ds->key->used) {
|
||||
size_t j;
|
||||
buffer_reset(srv->tmp_buf);
|
||||
buffer_prepare_append(srv->tmp_buf, ds->key->used + 2);
|
||||
for (j = 0; j < ds->key->used - 1; j++) {
|
||||
srv->tmp_buf->ptr[srv->tmp_buf->used++] = light_isalnum((unsigned char)ds->key->ptr[j]) ? toupper((unsigned char)ds->key->ptr[j]) : '_';
|
||||
}
|
||||
|
||||
srv->tmp_buf->ptr[srv->tmp_buf->used++] = '\0';
|
||||
uwsgi_add_var(b, srv->tmp_buf->ptr, srv->tmp_buf->used-1, ds->value->ptr, ds->value->used-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uh = (uwsgi_header *) b->ptr ;
|
||||
uh->modifier1 = 0 ;
|
||||
uh->modifier1 = (uint8_t) 0 ;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh->pktsize = uwsgi_swap16((uint16_t) b->used - 4) ;
|
||||
#else
|
||||
uh->pktsize = b->used - 4 ;
|
||||
uh->modifier2 = 0 ;
|
||||
#endif
|
||||
uh->modifier2 = (uint8_t) 0 ;
|
||||
|
||||
log_error_write(srv, __FILE__, __LINE__, "sd", "uwsgi_header ", b->used - 4);
|
||||
b->used++; /* fix size */
|
||||
|
||||
hctx->wb->bytes_in += b->used -1 ;
|
||||
@@ -908,7 +964,6 @@ static int mod_uwsgi_patch_connection(server *srv, connection *con, plugin_data
|
||||
|
||||
PATCH(extensions);
|
||||
PATCH(debug);
|
||||
PATCH(balance);
|
||||
|
||||
/* skip the first, the global context */
|
||||
for (i = 1; i < srv->config_context->used; i++) {
|
||||
@@ -926,8 +981,6 @@ static int mod_uwsgi_patch_connection(server *srv, connection *con, plugin_data
|
||||
PATCH(extensions);
|
||||
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("uwsgi.debug"))) {
|
||||
PATCH(debug);
|
||||
} else if (buffer_is_equal_string(du->key, CONST_STR_LEN("uwsgi.balance"))) {
|
||||
PATCH(balance);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1097,7 +1150,6 @@ static handler_t uwsgi_handle_fdevent(void *s, void *ctx, int revents) {
|
||||
static handler_t mod_uwsgi_check_extension(server *srv, connection *con, void *p_d) {
|
||||
plugin_data *p = p_d;
|
||||
size_t s_len;
|
||||
unsigned long last_max = ULONG_MAX;
|
||||
int max_usage = INT_MAX;
|
||||
int ndx = -1;
|
||||
size_t k;
|
||||
@@ -1143,13 +1195,7 @@ static handler_t mod_uwsgi_check_extension(server *srv, connection *con, void *p
|
||||
/* check extension in the form "/uwsgi_pattern" */
|
||||
if (*(ext->key->ptr) == '/') {
|
||||
if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
|
||||
if (s_len > ct_len + 1) {
|
||||
char *pi_offset;
|
||||
|
||||
if (0 != (pi_offset = strchr(fn->ptr + ct_len + 1, '/'))) {
|
||||
path_info_offset = pi_offset - fn->ptr;
|
||||
}
|
||||
}
|
||||
path_info_offset = ct_len ;
|
||||
extension = ext;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
#include <kvm.h>
|
||||
#include <sys/user.h>
|
||||
#elif defined(__sun__)
|
||||
/* Terrible Hack !!! */
|
||||
#ifndef _LP64
|
||||
#undef _FILE_OFFSET_BITS
|
||||
#endif
|
||||
#include <procfs.h>
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#endif
|
||||
|
||||
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
#include <sys/sysctl.h>
|
||||
#endif
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
void log_request(struct wsgi_request *wsgi_req) {
|
||||
|
||||
// optimize this (please)
|
||||
char *time_request;
|
||||
time_t microseconds, microseconds2;
|
||||
int rlen;
|
||||
int app_req = -1;
|
||||
char *msg2 = " ";
|
||||
char *via = msg2;
|
||||
|
||||
char mempkt[4096];
|
||||
char logpkt[4096];
|
||||
|
||||
struct iovec logvec[4] ;
|
||||
int logvecpos = 0 ;
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
char *msg1 = " via sendfile() ";
|
||||
|
||||
struct uwsgi_app *wi;
|
||||
|
||||
if (wsgi_req->app_id >= 0) {
|
||||
wi = &uwsgi.wsgi_apps[wsgi_req->app_id];
|
||||
if (wi->requests > 0) {
|
||||
app_req = wi->requests;
|
||||
}
|
||||
}
|
||||
|
||||
if (wsgi_req->sendfile_fd > -1 && wsgi_req->sendfile_obj == wsgi_req->async_result) { //wsgi_req->sendfile_fd_size > 0 ) {
|
||||
via = msg1;
|
||||
}
|
||||
#endif
|
||||
|
||||
time_request = ctime((const time_t *) &wsgi_req->start_of_request.tv_sec);
|
||||
microseconds = wsgi_req->end_of_request.tv_sec * 1000000 + wsgi_req->end_of_request.tv_usec;
|
||||
microseconds2 = wsgi_req->start_of_request.tv_sec * 1000000 + wsgi_req->start_of_request.tv_usec;
|
||||
|
||||
if (uwsgi.vhost) {
|
||||
logvec[logvecpos].iov_base = wsgi_req->host ;
|
||||
logvec[logvecpos].iov_len = wsgi_req->host_len ;
|
||||
logvecpos++;
|
||||
|
||||
logvec[logvecpos].iov_base = " " ;
|
||||
logvec[logvecpos].iov_len = 1 ;
|
||||
logvecpos++;
|
||||
}
|
||||
|
||||
if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1) {
|
||||
#ifndef UNBIT
|
||||
rlen = snprintf(mempkt, 4096, "{address space usage: %lld bytes/%lluMB} {rss usage: %llu bytes/%lluMB} ",
|
||||
(unsigned long long) uwsgi.workers[uwsgi.mywid].vsz_size, (unsigned long long ) uwsgi.workers[uwsgi.mywid].vsz_size / 1024 / 1024,
|
||||
(unsigned long long) uwsgi.workers[uwsgi.mywid].rss_size, (unsigned long long ) uwsgi.workers[uwsgi.mywid].rss_size / 1024 / 1024);
|
||||
#else
|
||||
rlen = snprintf(mempkt, 4096, "{address space usage: %lld bytes/%lluMB} ",
|
||||
(unsigned long long) uwsgi.workers[uwsgi.mywid].vsz_size, (unsigned long long) uwsgi.workers[uwsgi.mywid].vsz_size / 1024 / 1024);
|
||||
#endif
|
||||
|
||||
|
||||
logvec[logvecpos].iov_base = mempkt ;
|
||||
logvec[logvecpos].iov_len = rlen ;
|
||||
logvecpos++;
|
||||
|
||||
|
||||
}
|
||||
|
||||
rlen = snprintf(logpkt, 4096, "[pid: %d|app: %d|req: %d/%llu] %.*s (%.*s) {%d vars in %d bytes} [%.*s] %.*s %.*s => generated %llu bytes in %ld msecs%s(%.*s %d) %d headers in %d bytes (%d async switches on async core %d)\n",
|
||||
(int) uwsgi.mypid,
|
||||
wsgi_req->app_id,
|
||||
app_req,
|
||||
(unsigned long long ) uwsgi.workers[0].requests,
|
||||
wsgi_req->remote_addr_len, wsgi_req->remote_addr,
|
||||
wsgi_req->remote_user_len, wsgi_req->remote_user,
|
||||
wsgi_req->var_cnt,
|
||||
wsgi_req->uh.pktsize,
|
||||
24, time_request,
|
||||
wsgi_req->method_len, wsgi_req->method,
|
||||
wsgi_req->uri_len, wsgi_req->uri,
|
||||
(unsigned long long) wsgi_req->response_size,
|
||||
(long int) (microseconds - microseconds2) / 1000,
|
||||
via,
|
||||
wsgi_req->protocol_len, wsgi_req->protocol,
|
||||
wsgi_req->status,
|
||||
wsgi_req->header_cnt,
|
||||
wsgi_req->headers_size,
|
||||
wsgi_req->async_switches, wsgi_req->async_id);
|
||||
|
||||
logvec[logvecpos].iov_base = logpkt ;
|
||||
logvec[logvecpos].iov_len = rlen ;
|
||||
|
||||
// do not check for errors
|
||||
rlen = writev(2, logvec, logvecpos+1);
|
||||
|
||||
}
|
||||
|
||||
void get_memusage() {
|
||||
|
||||
#ifdef UNBIT
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = syscall(356);
|
||||
#elif defined(__linux__)
|
||||
FILE *procfile;
|
||||
int i;
|
||||
procfile = fopen("/proc/self/stat", "r");
|
||||
if (procfile) {
|
||||
i = fscanf(procfile, "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %llu %lld", (unsigned long long *) &uwsgi.workers[uwsgi.mywid].vsz_size, (unsigned long long *) &uwsgi.workers[uwsgi.mywid].rss_size);
|
||||
if (i != 2) {
|
||||
uwsgi_log( "warning: invalid record in /proc/self/stat\n");
|
||||
}
|
||||
fclose(procfile);
|
||||
}
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = uwsgi.workers[uwsgi.mywid].rss_size * uwsgi.page_size;
|
||||
#elif defined (__sun__)
|
||||
psinfo_t info;
|
||||
int procfd ;
|
||||
|
||||
procfd = open("/proc/self/psinfo", O_RDONLY);
|
||||
if (procfd >= 0) {
|
||||
if ( read(procfd, (char *) &info, sizeof(info)) > 0) {
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = (uint64_t) info.pr_rssize * 1024 ;
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = (uint64_t) info.pr_size * 1024 ;
|
||||
}
|
||||
close(procfd);
|
||||
}
|
||||
|
||||
#elif defined( __APPLE__)
|
||||
/* darwin documentation says that the value are in pages, but they are bytes !!! */
|
||||
struct task_basic_info t_info;
|
||||
mach_msg_type_number_t t_size = sizeof(struct task_basic_info);
|
||||
|
||||
if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t) & t_info, &t_size) == KERN_SUCCESS) {
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = t_info.resident_size;
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = t_info.virtual_size;
|
||||
}
|
||||
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
kvm_t *kv;
|
||||
int cnt;
|
||||
|
||||
kv = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
|
||||
if (kv) {
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
|
||||
struct kinfo_proc *kproc;
|
||||
kproc = kvm_getprocs(kv, KERN_PROC_PID, uwsgi.mypid, &cnt);
|
||||
if (kproc && cnt > 0) {
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = kproc->ki_size;
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = kproc->ki_rssize * uwsgi.page_size;
|
||||
}
|
||||
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
||||
struct kinfo_proc2 *kproc2;
|
||||
|
||||
kproc2 = kvm_getproc2(kv, KERN_PROC_PID, uwsgi.mypid, sizeof(struct kinfo_proc2), &cnt);
|
||||
if (kproc2 && cnt > 0) {
|
||||
#ifdef __OpenBSD__
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = (kproc2->p_vm_dsize + kproc2->p_vm_ssize + kproc2->p_vm_tsize) * uwsgi.page_size;
|
||||
#else
|
||||
uwsgi.workers[uwsgi.mywid].vsz_size = kproc2->p_vm_msize * uwsgi.page_size;
|
||||
#endif
|
||||
uwsgi.workers[uwsgi.mywid].rss_size = kproc2->p_vm_rssize * uwsgi.page_size;
|
||||
}
|
||||
#endif
|
||||
|
||||
kvm_close(kv);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.8 KiB |
@@ -0,0 +1,22 @@
|
||||
<unbit>
|
||||
<pippo/>
|
||||
|
||||
<pluto>
|
||||
ciao
|
||||
ciao
|
||||
</pluto>
|
||||
|
||||
|
||||
<uwsgi id="unbit.it">
|
||||
<http>:3333</http>
|
||||
<module>trac.web.main:dispatch_request</module>
|
||||
<pluto>ciao</pluto>
|
||||
<env>TRAC_ENV=/root/uwsgi/utrac</env>
|
||||
</uwsgi>
|
||||
|
||||
<uwsgi id="projects.unbit.it">
|
||||
<http>:4444</http>
|
||||
<module>trac.web.main:dispatch_request</module>
|
||||
</uwsgi>
|
||||
|
||||
</unbit>
|
||||
@@ -0,0 +1,26 @@
|
||||
import uwsgi
|
||||
|
||||
import os
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
boundary = 'uwsgi_mjpeg_frame'
|
||||
|
||||
start_response('200 Ok', [
|
||||
|
||||
('Cache-Control', 'no-cache'),
|
||||
('Cache-Control', 'private'),
|
||||
('Pragma', 'no-cache'),
|
||||
('Content-Type', 'multipart/x-mixed-replace; boundary=' + boundary),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
yield "--%s\n" % boundary
|
||||
|
||||
while 1:
|
||||
yield "Content-Type: image/jpeg\r\n\r\n"
|
||||
uwsgi.sendfile('screenshot.jpg')
|
||||
yield "--%s\n" % boundary
|
||||
#os.system('./isightcapture -w 640 -h 480 screenshot.jpg')
|
||||
#os.system('screencapture -m -T 1 screenshot.jpg')
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
use Mojolicious::Lite;
|
||||
|
||||
# /
|
||||
get '/' => 'index';
|
||||
|
||||
# /*
|
||||
get '/:groovy' => sub {
|
||||
my $self = shift;
|
||||
$self->render_text($self->param('groovy'), layout => 'funky');
|
||||
};
|
||||
|
||||
app->start('psgi');
|
||||
__DATA__
|
||||
|
||||
@@ index.html.ep
|
||||
% layout 'funky';
|
||||
Yea baby!
|
||||
|
||||
@@ layouts/funky.html.ep
|
||||
<!doctype html><html>
|
||||
<head><title>Funky!</title></head>
|
||||
<body><%= content %></body>
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
import uwsgi
|
||||
import struct
|
||||
import sys
|
||||
|
||||
|
||||
print uwsgi.send_uwsgi_message(sys.argv[1], int(sys.argv[2]), 10, int(sys.argv[3]), struct.pack("i", int(sys.argv[4])))
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
void nagios(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct pollfd nagios_poll;
|
||||
// connect and send
|
||||
if (uwsgi->socket_name == NULL) {
|
||||
fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n");
|
||||
exit(3);
|
||||
}
|
||||
char *tcp_port = strchr(uwsgi->socket_name, ':');
|
||||
if (tcp_port == NULL) {
|
||||
fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n");
|
||||
exit(3);
|
||||
}
|
||||
|
||||
tcp_port[0] = 0;
|
||||
|
||||
nagios_poll.fd = connect_to_tcp(uwsgi->socket_name, atoi(tcp_port + 1), uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);
|
||||
if (nagios_poll.fd < 0) {
|
||||
fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers\n");
|
||||
exit(2);
|
||||
}
|
||||
uwsgi->wsgi_req->uh.modifier1 = UWSGI_MODIFIER_PING;
|
||||
uwsgi->wsgi_req->uh.pktsize = 0;
|
||||
uwsgi->wsgi_req->uh.modifier2 = 0;
|
||||
if (write(nagios_poll.fd, uwsgi->wsgi_req, 4) != 4) {
|
||||
uwsgi_error("write()");
|
||||
fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n");
|
||||
exit(2);
|
||||
}
|
||||
nagios_poll.events = POLLIN;
|
||||
if (!uwsgi_parse_response(&nagios_poll, uwsgi->shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) uwsgi->wsgi_req, uwsgi->wsgi_req->buffer)) {
|
||||
fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");
|
||||
exit(2);
|
||||
}
|
||||
else {
|
||||
if (uwsgi->wsgi_req->uh.pktsize > 0) {
|
||||
fprintf(stdout, "UWSGI WARNING: %.*s\n", uwsgi->wsgi_req->uh.pktsize, uwsgi->wsgi_req->buffer);
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
fprintf(stdout, "UWSGI OK: armed and ready\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
// never here
|
||||
fprintf(stdout, "UWSGI UNKNOWN: probably you hit a bug of uWSGI !!!\n");
|
||||
exit(3);
|
||||
}
|
||||
+1018
-937
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,81 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
void embed_plugins(struct uwsgi_server *uwsgi) {
|
||||
|
||||
#ifdef UWSGI_EMBED_PLUGIN_PSGI
|
||||
if (uwsgi->plugin_arg_psgi)
|
||||
ret = uwsgi_load_plugin(uwsgi, 5, "psgi_plugin.so", uwsgi->plugin_arg_psgi, 0);
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_EMBED_PLUGIN_LUA
|
||||
if (uwsgi->plugin_arg_lua)
|
||||
ret = uwsgi_load_plugin(uwsgi, 6, "lua_plugin.so", uwsgi->plugin_arg_lua, 0);
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_EMBED_PLUGIN_RACK
|
||||
if (uwsgi->plugin_arg_rack)
|
||||
ret = uwsgi_load_plugin(uwsgi, 7, "rack_plugin.so", uwsgi->plugin_arg_rack, 0);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_load_plugin(struct uwsgi_server *uwsgi, int modifier, char *plugin, char *pargs, int absolute) {
|
||||
|
||||
char *plugin_name ;
|
||||
|
||||
void *plugin_handle;
|
||||
int (*plugin_init) (struct uwsgi_server *, char *);
|
||||
int (*plugin_request) (struct uwsgi_server *, struct wsgi_request *);
|
||||
void (*plugin_after_request) (struct uwsgi_server *, struct wsgi_request *);
|
||||
|
||||
if (absolute) {
|
||||
plugin_name = malloc(strlen(plugin) + 1);
|
||||
memcpy(plugin_name, plugin, strlen(plugin) + 1);
|
||||
}
|
||||
else {
|
||||
plugin_name = malloc(strlen(UWSGI_PLUGIN_DIR) + 1 + strlen(plugin) + 1);
|
||||
if (!plugin_name) {
|
||||
uwsgi_error("malloc()");
|
||||
return -1 ;
|
||||
}
|
||||
memcpy(plugin_name, UWSGI_PLUGIN_DIR, strlen(UWSGI_PLUGIN_DIR));
|
||||
memcpy(plugin_name + strlen(UWSGI_PLUGIN_DIR) , "/", 1);
|
||||
memcpy(plugin_name + strlen(UWSGI_PLUGIN_DIR) + 1, plugin, strlen(plugin) + 1);
|
||||
}
|
||||
plugin_handle = dlopen(plugin_name, RTLD_NOW | RTLD_GLOBAL);
|
||||
free(plugin_name);
|
||||
|
||||
if (!plugin_handle) {
|
||||
uwsgi_log( "%s\n", dlerror());
|
||||
}
|
||||
else {
|
||||
plugin_init = dlsym(plugin_handle, "uwsgi_init");
|
||||
if (plugin_init) {
|
||||
if ((*plugin_init) (uwsgi, pargs)) {
|
||||
uwsgi_log( "plugin initialization returned error\n");
|
||||
if (dlclose(plugin_handle)) {
|
||||
uwsgi_log( "unable to unload plugin\n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
plugin_request = dlsym(plugin_handle, "uwsgi_request");
|
||||
if (plugin_request) {
|
||||
uwsgi->shared->hooks[modifier] = plugin_request;
|
||||
plugin_after_request = dlsym(plugin_handle, "uwsgi_after_request");
|
||||
if (plugin_after_request) {
|
||||
uwsgi->shared->after_hooks[modifier] = plugin_after_request;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "%s\n", dlerror());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "../../uwsgi.h"
|
||||
|
||||
int uwsgi_init(struct uwsgi_server *uwsgi, char *args){
|
||||
uwsgi_log("i am the example plugin initialization function with arg: %s\n", args);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uwsgi_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
char *http = "HTTP/1.1 200 Ok\r\nContent-type: text/html\r\n\r\n<h1>Hello World</h1>" ;
|
||||
|
||||
wsgi_req->response_size += write(wsgi_req->poll.fd, http, strlen(http));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_after_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
uwsgi_log("i am the example plugin after request function\n");
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
NAME='example'
|
||||
CFLAGS = ''
|
||||
LDFLAGS = ''
|
||||
@@ -0,0 +1,213 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
/* gcc -fPIC -shared -o lua_plugin.so `python2.5-config --cflags` -I /usr/include/lua5.1 -llua5.1 lua_plugin.c */
|
||||
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
|
||||
|
||||
static struct uwsgi_lua {
|
||||
struct lua_State *L ;
|
||||
struct uwsgi_server *uwsgi;
|
||||
} ulua;
|
||||
|
||||
static void *uwsgi_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
if(nsize == 0) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return realloc(ptr, nsize);
|
||||
}
|
||||
|
||||
static const char *uwsgi_read_lua(lua_State *L, void *data, size_t *size) {
|
||||
|
||||
int fd = *(int *)data;
|
||||
static char buf[4096];
|
||||
|
||||
*size = read(fd, buf, 4096);
|
||||
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int uwsgi_lua_input(lua_State *L) {
|
||||
|
||||
int n = lua_gettop(L);
|
||||
|
||||
static char *buf = "" ;
|
||||
|
||||
fprintf(stderr,"richiesti %d bytes\n", n);
|
||||
|
||||
//sum = read(ulua.uwsgi->poll.fd,);
|
||||
|
||||
lua_pushlstring(L, buf, 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int uwsgi_init(struct uwsgi_server *uwsgi, char *args){
|
||||
|
||||
int fd, i;
|
||||
|
||||
fprintf(stderr,"Initializing Lua environment...\n");
|
||||
|
||||
|
||||
|
||||
|
||||
ulua.L = lua_newstate(uwsgi_lua_alloc, NULL);
|
||||
|
||||
luaL_openlibs(ulua.L);
|
||||
|
||||
|
||||
fd = open(args, O_RDONLY) ;
|
||||
if (fd < 0) {
|
||||
perror("open()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
|
||||
i = lua_load(ulua.L, uwsgi_read_lua, &fd, "uwsgi");
|
||||
|
||||
fprintf(stderr,"lua_load: %d\n" ,i);
|
||||
|
||||
// use a pcall
|
||||
lua_call(ulua.L, 0, 1);
|
||||
|
||||
fprintf(stderr,"%s\n", lua_typename(ulua.L, lua_type(ulua.L, -1)));
|
||||
|
||||
// ok the lua engine is ready
|
||||
return 0 ;
|
||||
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int i;
|
||||
const char *http ;
|
||||
size_t slen ;
|
||||
char *ptrbuf;
|
||||
|
||||
/* Standard WSAPI request */
|
||||
if (!wsgi_req->uh.pktsize) {
|
||||
fprintf (stderr, "Invalid WSAPI request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi_parse_vars(uwsgi, wsgi_req)) {
|
||||
fprintf(stderr,"Invalid WSAPI request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// put function in the stack
|
||||
lua_getfield(ulua.L, LUA_GLOBALSINDEX, "run");
|
||||
|
||||
// put cgi vars in the stack
|
||||
lua_newtable(ulua.L);
|
||||
lua_pushstring(ulua.L, "");
|
||||
lua_setfield(ulua.L, -2, "CONTENT_TYPE");
|
||||
for(i=0;i<wsgi_req->var_cnt;i++) {
|
||||
lua_pushlstring(ulua.L, (char *)wsgi_req->hvec[i+1].iov_base, wsgi_req->hvec[i+1].iov_len);
|
||||
// transform it in a valid c string TODO this is ugly
|
||||
ptrbuf = wsgi_req->hvec[i].iov_base+wsgi_req->hvec[i].iov_len ;
|
||||
*ptrbuf = 0 ;
|
||||
lua_setfield(ulua.L, -2, (char *)wsgi_req->hvec[i].iov_base);
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
// put "input" table
|
||||
lua_newtable(ulua.L);
|
||||
lua_pushcfunction(ulua.L, uwsgi_lua_input);
|
||||
lua_setfield(ulua.L, -2, "read");
|
||||
lua_setfield(ulua.L, -2, "input");
|
||||
|
||||
|
||||
// call function
|
||||
i = lua_pcall(ulua.L, 1, 3, 0);
|
||||
if (i != 0) {
|
||||
fprintf(stderr,"%s\n", lua_tostring(ulua.L, -1));
|
||||
lua_pop(ulua.L, 1);
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
fprintf(stderr,"%d %s %s %s\n",i,lua_typename(ulua.L, lua_type(ulua.L, -3)), lua_typename(ulua.L, lua_type(ulua.L, -2)) , lua_typename(ulua.L, lua_type(ulua.L, -1)));
|
||||
|
||||
// send status
|
||||
if (lua_type(ulua.L, -3) == LUA_TSTRING || lua_type(ulua.L, -3) == LUA_TNUMBER) {
|
||||
http = lua_tolstring(ulua.L, -3, &slen);
|
||||
if (write(wsgi_req->poll.fd, "HTTP/1.1 ", 9) != 9) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
if (write(wsgi_req->poll.fd, http, slen) != slen) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
if (write(wsgi_req->poll.fd, "\r\n", 2) != 2) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
}
|
||||
|
||||
// send headers
|
||||
|
||||
lua_pushnil(ulua.L);
|
||||
while(lua_next(ulua.L, -3) != 0) {
|
||||
http = lua_tolstring(ulua.L, -2, &slen);
|
||||
if (write(wsgi_req->poll.fd, http, slen) != slen) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
if (write(wsgi_req->poll.fd, ": ", 2) != 2) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
http = lua_tolstring(ulua.L, -1, &slen);
|
||||
if (write(wsgi_req->poll.fd, http, slen) != slen) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
if (write(wsgi_req->poll.fd, "\r\n", 2) != 2) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
lua_pop(ulua.L, 1);
|
||||
}
|
||||
|
||||
if (write(wsgi_req->poll.fd, "\r\n", 2) != 2) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
// send body with coroutine
|
||||
lua_pushvalue(ulua.L, -1);
|
||||
|
||||
while ( (i = lua_pcall(ulua.L, 0, 1, 0)) == 0) {
|
||||
if (lua_type(ulua.L, -1) == LUA_TSTRING) {
|
||||
http = lua_tolstring(ulua.L, -1, &slen);
|
||||
if (write(wsgi_req->poll.fd, http, slen) != slen) {
|
||||
perror("write()");
|
||||
return -1 ;
|
||||
}
|
||||
//fprintf(stderr,"%.*s\n", slen, http);
|
||||
}
|
||||
lua_pop(ulua.L, 1);
|
||||
lua_pushvalue(ulua.L, -1);
|
||||
}
|
||||
|
||||
lua_pop(ulua.L, 4);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_after_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
if (uwsgi->shared->options[UWSGI_OPTION_LOGGING])
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import os,sys
|
||||
|
||||
NAME='lua'
|
||||
CFLAGS = '-I /usr/include/lua5.1'
|
||||
LDFLAGS = '-llua5.1'
|
||||
@@ -0,0 +1,379 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
#include <EXTERN.h>
|
||||
#include <perl.h>
|
||||
|
||||
static PerlInterpreter *my_perl;
|
||||
static SV *psgi_func ;
|
||||
|
||||
extern char **environ;
|
||||
|
||||
/* statistically ordered */
|
||||
static struct http_status_codes hsc[] = {
|
||||
|
||||
{"200", "OK"},
|
||||
{"302", "Found"},
|
||||
{"404", "Not Found"},
|
||||
{"500", "Internal Server Error"},
|
||||
{"301", "Moved Permanently"},
|
||||
{"304", "Not Modified"},
|
||||
{"303", "See Other"},
|
||||
{"403", "Forbidden"},
|
||||
{"307", "Temporary Redirect"},
|
||||
{"401", "Unauthorized"},
|
||||
{"400", "Bad Request"},
|
||||
{"405", "Method Not Allowed"},
|
||||
{"408", "Request Timeout"},
|
||||
|
||||
{"100", "Continue"},
|
||||
{"101", "Switching Protocols"},
|
||||
{"201", "Created"},
|
||||
{"202", "Accepted"},
|
||||
{"203", "Non-Authoritative Information"},
|
||||
{"204", "No Content"},
|
||||
{"205", "Reset Content"},
|
||||
{"206", "Partial Content"},
|
||||
{"300", "Multiple Choices"},
|
||||
{"305", "Use Proxy"},
|
||||
{"402", "Payment Required"},
|
||||
{"406", "Not Acceptable"},
|
||||
{"407", "Proxy Authentication Required"},
|
||||
{"409", "Conflict"},
|
||||
{"410", "Gone"},
|
||||
{"411", "Length Required"},
|
||||
{"412", "Precondition Failed"},
|
||||
{"413", "Request Entity Too Large"},
|
||||
{"414", "Request-URI Too Long"},
|
||||
{"415", "Unsupported Media Type"},
|
||||
{"416", "Requested Range Not Satisfiable"},
|
||||
{"417", "Expectation Failed"},
|
||||
{"501", "Not Implemented"},
|
||||
{"502", "Bad Gateway"},
|
||||
{"503", "Service Unavailable"},
|
||||
{"504", "Gateway Timeout"},
|
||||
{"505", "HTTP Version Not Supported"},
|
||||
{ "", NULL },
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* automatically generated */
|
||||
|
||||
EXTERN_C void xs_init (pTHX);
|
||||
|
||||
EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
|
||||
|
||||
EXTERN_C void
|
||||
xs_init(pTHX)
|
||||
{
|
||||
char *file = __FILE__;
|
||||
dXSUB_SYS;
|
||||
|
||||
/* DynaLoader is a special case */
|
||||
newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
|
||||
}
|
||||
|
||||
/* end of automagically generated part */
|
||||
|
||||
|
||||
int uwsgi_init(struct uwsgi_server *uwsgi, char *args){
|
||||
|
||||
char *psgibuffer ;
|
||||
int fd ;
|
||||
|
||||
struct stat stat_psgi;
|
||||
|
||||
struct http_status_codes *http_sc ;
|
||||
|
||||
int argc = 4 ;
|
||||
char *embedding[] = { "", args, "-e", "0" };
|
||||
char **argv = embedding ;
|
||||
|
||||
uwsgi_log("initializing Perl environment: %s\n", args);
|
||||
|
||||
PERL_SYS_INIT3(&argc, &argv, &environ);
|
||||
my_perl = perl_alloc();
|
||||
if (!my_perl) {
|
||||
uwsgi_log("unable to allocate perl interpreter\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
PL_perl_destruct_level = 1;
|
||||
perl_construct(my_perl);
|
||||
|
||||
// filling http status codes
|
||||
for (http_sc = hsc; http_sc->message != NULL; http_sc++) {
|
||||
http_sc->message_size = strlen(http_sc->message);
|
||||
}
|
||||
|
||||
|
||||
PL_origalen = 1;
|
||||
perl_parse(my_perl, xs_init, 4, embedding, NULL);
|
||||
|
||||
perl_eval_pv("use IO::Handle;", 0);
|
||||
|
||||
fd = open(args, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("open()");
|
||||
goto clear ;
|
||||
}
|
||||
|
||||
if (fstat(fd, &stat_psgi)) {
|
||||
uwsgi_error("fstat()");
|
||||
close(fd);
|
||||
goto clear;
|
||||
}
|
||||
|
||||
psgibuffer = malloc(stat_psgi.st_size);
|
||||
if (!psgibuffer) {
|
||||
uwsgi_error("malloc()");
|
||||
close(fd);
|
||||
goto clear;
|
||||
}
|
||||
|
||||
if (read(fd, psgibuffer, stat_psgi.st_size) != stat_psgi.st_size) {
|
||||
uwsgi_error("read()");
|
||||
close(fd);
|
||||
free(psgibuffer);
|
||||
goto clear;
|
||||
}
|
||||
|
||||
psgibuffer[stat_psgi.st_size] = 0 ;
|
||||
|
||||
psgi_func = perl_eval_pv(psgibuffer, 0);
|
||||
|
||||
if (!psgi_func) {
|
||||
uwsgi_log("unable to find PSGI function entry point.\n");
|
||||
close(fd);
|
||||
free(psgibuffer);
|
||||
goto clear;
|
||||
}
|
||||
|
||||
free(psgibuffer);
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
|
||||
clear:
|
||||
perl_destruct(my_perl);
|
||||
perl_free(my_perl);
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
int uwsgi_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
HV *env ;
|
||||
SV **item ;
|
||||
|
||||
AV *response, *headers, *body ;
|
||||
|
||||
SV **status_code, **hitem, *io_new, *io_err, *io, *chunk;
|
||||
char *chitem ;
|
||||
STRLEN hlen ;
|
||||
|
||||
struct http_status_codes *http_sc;
|
||||
|
||||
int i,vi, base ;
|
||||
|
||||
/* Standard PSGI request */
|
||||
if (!wsgi_req->uh.pktsize) {
|
||||
uwsgi_log("Invalid PSGI request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (uwsgi_parse_vars(uwsgi, wsgi_req)) {
|
||||
uwsgi_log("Invalid PSGI request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
dSP;
|
||||
|
||||
ENTER;
|
||||
SAVETMPS;
|
||||
|
||||
|
||||
env = (HV*)sv_2mortal((SV*)newHV());
|
||||
|
||||
// fill perl hash
|
||||
for(i=0;i<wsgi_req->var_cnt;i++) {
|
||||
if (wsgi_req->hvec[i+1].iov_len > 0) {
|
||||
|
||||
item = hv_store(env, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len,
|
||||
newSVpv(wsgi_req->hvec[i+1].iov_base, wsgi_req->hvec[i+1].iov_len), 0);
|
||||
}
|
||||
else {
|
||||
item = hv_store(env, wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len, newSVpv("", 0), 0);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
item = hv_store(env, "psgi.url_scheme", 15, newSVpv("http", 4), 0);
|
||||
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs( sv_2mortal( newSVpv( "IO::Handle", 10 )));
|
||||
PUTBACK;
|
||||
perl_call_method( "new", G_SCALAR);
|
||||
SPAGAIN;
|
||||
io_new = newSVsv(POPs);
|
||||
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs( sv_2mortal(io_new) );
|
||||
XPUSHs( sv_2mortal( newSViv( wsgi_req->poll.fd)));
|
||||
XPUSHs( sv_2mortal( newSVpv( "r", 1)));
|
||||
PUTBACK;
|
||||
perl_call_method( "fdopen", G_SCALAR);
|
||||
SPAGAIN;
|
||||
|
||||
|
||||
item = hv_store(env, "psgi.input", 10, newSVsv(POPs), 0);
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs( sv_2mortal( newSVpv( "IO::Handle", 10 )));
|
||||
PUTBACK;
|
||||
perl_call_method( "new", G_SCALAR);
|
||||
SPAGAIN;
|
||||
io_err = newSVsv(POPs);
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs( sv_2mortal(io_err) );
|
||||
XPUSHs( sv_2mortal( newSViv( 2 )));
|
||||
XPUSHs( sv_2mortal( newSVpv( "w", 1)));
|
||||
PUTBACK;
|
||||
perl_call_method( "fdopen", G_SCALAR);
|
||||
SPAGAIN;
|
||||
|
||||
|
||||
item = hv_store(env, "psgi.errors", 11, newSVsv(POPs), 0);
|
||||
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs( sv_2mortal(newRV((SV *)env )) );
|
||||
PUTBACK;
|
||||
|
||||
|
||||
perl_call_sv(psgi_func, G_SCALAR);
|
||||
SPAGAIN;
|
||||
|
||||
// dereference output
|
||||
response = (AV *) SvRV( sv_2mortal(newSVsv(POPs)) ) ;
|
||||
|
||||
status_code = av_fetch(response, 0, 0);
|
||||
|
||||
wsgi_req->hvec[0].iov_base = "HTTP/1.1 ";
|
||||
wsgi_req->hvec[0].iov_len = 9 ;
|
||||
|
||||
wsgi_req->hvec[1].iov_base = SvPV(*status_code, hlen);
|
||||
wsgi_req->hvec[1].iov_len = 3 ;
|
||||
|
||||
wsgi_req->hvec[2].iov_base = " ";
|
||||
wsgi_req->hvec[2].iov_len = 1 ;
|
||||
|
||||
wsgi_req->hvec[3].iov_len = 0 ;
|
||||
|
||||
// get the status code
|
||||
for (http_sc = hsc; http_sc->message != NULL; http_sc++) {
|
||||
if (!strncmp(http_sc->key, wsgi_req->hvec[1].iov_base, 3)) {
|
||||
wsgi_req->hvec[3].iov_base = http_sc->message ;
|
||||
wsgi_req->hvec[3].iov_len = http_sc->message_size ;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (wsgi_req->hvec[3].iov_len == 0) {
|
||||
wsgi_req->hvec[3].iov_base = "Unknown" ;
|
||||
wsgi_req->hvec[3].iov_len = 7;
|
||||
}
|
||||
|
||||
wsgi_req->hvec[4].iov_base = "\r\n";
|
||||
wsgi_req->hvec[4].iov_len = 2 ;
|
||||
|
||||
hitem = av_fetch(response, 1, 0) ;
|
||||
|
||||
headers = (AV *) SvRV(*hitem);
|
||||
|
||||
base = 5 ;
|
||||
|
||||
// put them in hvec
|
||||
for(i=0; i<=av_len(headers); i++) {
|
||||
|
||||
vi = (i*2)+base ;
|
||||
hitem = av_fetch(headers,i,0);
|
||||
chitem = SvPV(*hitem, hlen);
|
||||
wsgi_req->hvec[vi].iov_base = chitem ; wsgi_req->hvec[vi].iov_len = hlen ;
|
||||
|
||||
wsgi_req->hvec[vi+1].iov_base = ": " ; wsgi_req->hvec[vi+1].iov_len = 2 ;
|
||||
|
||||
hitem = av_fetch(headers,i+1,0);
|
||||
chitem = SvPV(*hitem, hlen);
|
||||
wsgi_req->hvec[vi+2].iov_base = chitem ; wsgi_req->hvec[vi+2].iov_len = hlen ;
|
||||
|
||||
wsgi_req->hvec[vi+3].iov_base = "\r\n" ; wsgi_req->hvec[vi+3].iov_len = 2 ;
|
||||
|
||||
i++;
|
||||
}
|
||||
|
||||
vi = (i*2)+base ;
|
||||
wsgi_req->hvec[vi].iov_base = "\r\n" ; wsgi_req->hvec[vi].iov_len = 2 ;
|
||||
|
||||
|
||||
if ( (wsgi_req->response_size = writev(wsgi_req->poll.fd, wsgi_req->hvec, vi+1)) < 0) {
|
||||
uwsgi_error("writev()");
|
||||
}
|
||||
|
||||
|
||||
hitem = av_fetch(response, 2, 0) ;
|
||||
|
||||
io = *hitem;
|
||||
|
||||
if (SvTYPE(SvRV(io)) == SVt_PVGV || SvTYPE(SvRV(io)) == SVt_PVHV) {
|
||||
|
||||
for(;;) {
|
||||
|
||||
PUSHMARK(SP);
|
||||
XPUSHs(io) ;
|
||||
PUTBACK;
|
||||
perl_call_method("getline", G_SCALAR);
|
||||
SPAGAIN;
|
||||
chunk = sv_2mortal(newSVsv(POPs));
|
||||
|
||||
chitem = SvPV(chunk, hlen);
|
||||
if (hlen <= 0) {
|
||||
break;
|
||||
}
|
||||
wsgi_req->response_size += write(wsgi_req->poll.fd, chitem, hlen);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else if (SvTYPE(SvRV(io)) == SVt_PVAV) {
|
||||
|
||||
body = (AV *) SvRV(io);
|
||||
|
||||
for(i=0; i<=av_len(body); i++) {
|
||||
hitem = av_fetch(body,i,0);
|
||||
chitem = SvPV(*hitem, hlen);
|
||||
wsgi_req->response_size += write(wsgi_req->poll.fd, chitem, hlen);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
uwsgi_log("unsupported response body type: %d\n", SvTYPE(SvRV(io)));
|
||||
}
|
||||
|
||||
FREETMPS;
|
||||
LEAVE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_after_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
if (uwsgi->shared->options[UWSGI_OPTION_LOGGING])
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import os,sys
|
||||
|
||||
NAME='psgi'
|
||||
CFLAGS = os.popen('perl -MExtUtils::Embed -e ccopts').read().rstrip()
|
||||
LDFLAGS = os.popen('perl -MExtUtils::Embed -e ldopts').read().rstrip()
|
||||
@@ -0,0 +1,382 @@
|
||||
#include <uwsgi.h>
|
||||
|
||||
#include <ruby.h>
|
||||
|
||||
extern char **environ;
|
||||
|
||||
/* statistically ordered */
|
||||
static struct http_status_codes hsc[] = {
|
||||
|
||||
{"200", "OK"},
|
||||
{"302", "Found"},
|
||||
{"404", "Not Found"},
|
||||
{"500", "Internal Server Error"},
|
||||
{"301", "Moved Permanently"},
|
||||
{"304", "Not Modified"},
|
||||
{"303", "See Other"},
|
||||
{"403", "Forbidden"},
|
||||
{"307", "Temporary Redirect"},
|
||||
{"401", "Unauthorized"},
|
||||
{"400", "Bad Request"},
|
||||
{"405", "Method Not Allowed"},
|
||||
{"408", "Request Timeout"},
|
||||
|
||||
{"100", "Continue"},
|
||||
{"101", "Switching Protocols"},
|
||||
{"201", "Created"},
|
||||
{"202", "Accepted"},
|
||||
{"203", "Non-Authoritative Information"},
|
||||
{"204", "No Content"},
|
||||
{"205", "Reset Content"},
|
||||
{"206", "Partial Content"},
|
||||
{"300", "Multiple Choices"},
|
||||
{"305", "Use Proxy"},
|
||||
{"402", "Payment Required"},
|
||||
{"406", "Not Acceptable"},
|
||||
{"407", "Proxy Authentication Required"},
|
||||
{"409", "Conflict"},
|
||||
{"410", "Gone"},
|
||||
{"411", "Length Required"},
|
||||
{"412", "Precondition Failed"},
|
||||
{"413", "Request Entity Too Large"},
|
||||
{"414", "Request-URI Too Long"},
|
||||
{"415", "Unsupported Media Type"},
|
||||
{"416", "Requested Range Not Satisfiable"},
|
||||
{"417", "Expectation Failed"},
|
||||
{"501", "Not Implemented"},
|
||||
{"502", "Bad Gateway"},
|
||||
{"503", "Service Unavailable"},
|
||||
{"504", "Gateway Timeout"},
|
||||
{"505", "HTTP Version Not Supported"},
|
||||
{ "", NULL },
|
||||
};
|
||||
|
||||
|
||||
VALUE dispatcher;
|
||||
VALUE rb_uwsgi_io_class;
|
||||
ID call;
|
||||
|
||||
VALUE rb_uwsgi_io_new(VALUE class, VALUE wr) {
|
||||
|
||||
struct wsgi_request *wsgi_req;
|
||||
|
||||
Data_Get_Struct(wr, struct wsgi_request, wsgi_req);
|
||||
uwsgi_log("INITIALIZING UWSGI IO FOR FD: %d\n", wsgi_req->poll.fd);
|
||||
|
||||
VALUE self = Data_Wrap_Struct(class , 0, 0, wsgi_req);
|
||||
|
||||
rb_obj_call_init(self, 0, NULL);
|
||||
|
||||
return self;
|
||||
|
||||
}
|
||||
|
||||
VALUE rb_uwsgi_io_init(int argc, VALUE *argv, VALUE self) {
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
VALUE rb_uwsgi_io_gets(VALUE obj, VALUE args) {
|
||||
|
||||
struct wsgi_request *wsgi_req;
|
||||
Data_Get_Struct(obj, struct wsgi_request, wsgi_req);
|
||||
|
||||
uwsgi_log("CALLING gets !!!\n");
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE rb_uwsgi_io_each(VALUE obj, VALUE args) {
|
||||
|
||||
struct wsgi_request *wsgi_req;
|
||||
Data_Get_Struct(obj, struct wsgi_request, wsgi_req);
|
||||
|
||||
uwsgi_log("CALLING each !!!\n");
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE rb_uwsgi_io_read(VALUE obj, VALUE args) {
|
||||
|
||||
struct wsgi_request *wsgi_req;
|
||||
Data_Get_Struct(obj, struct wsgi_request, wsgi_req);
|
||||
|
||||
size_t len;
|
||||
|
||||
uwsgi_log("CALLING read on %d !!!\n", wsgi_req->poll.fd);
|
||||
|
||||
if (RARRAY(args)->len == 0) {
|
||||
uwsgi_log("READ ALL THE BODY (%d bytes)\n", wsgi_req->post_cl);
|
||||
char *post_body = malloc(wsgi_req->post_cl);
|
||||
if (post_body) {
|
||||
len = read( wsgi_req->poll.fd, post_body, wsgi_req->post_cl);
|
||||
VALUE chunk = rb_str_new(post_body, wsgi_req->post_cl);
|
||||
free(post_body);
|
||||
return chunk;
|
||||
}
|
||||
}
|
||||
else if (RARRAY(args)->len > 0) {
|
||||
|
||||
uwsgi_log("CHUNK SIZE: %d\n", NUM2INT( RARRAY(args)->ptr[0] ) );
|
||||
}
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE rb_uwsgi_io_rewind(VALUE obj, VALUE args) {
|
||||
|
||||
struct wsgi_request *wsgi_req;
|
||||
Data_Get_Struct(obj, struct wsgi_request, wsgi_req);
|
||||
|
||||
uwsgi_log("CALLING rewind !!!\n");
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
int uwsgi_init(struct uwsgi_server *uwsgi, char *args){
|
||||
|
||||
struct http_status_codes *http_sc;
|
||||
|
||||
// filling http status codes
|
||||
for (http_sc = hsc; http_sc->message != NULL; http_sc++) {
|
||||
http_sc->message_size = strlen(http_sc->message);
|
||||
}
|
||||
|
||||
ruby_init();
|
||||
ruby_init_loadpath();
|
||||
ruby_script("uwsgi");
|
||||
|
||||
if (chdir("mytipo")) {
|
||||
uwsgi_error("chdir()");
|
||||
}
|
||||
|
||||
rb_require("config/environment");
|
||||
|
||||
uwsgi_log("ruby ready\n");
|
||||
|
||||
VALUE ac = rb_const_get(rb_cObject, rb_intern("ActionController")) ;
|
||||
|
||||
dispatcher = rb_funcall( rb_const_get(ac, rb_intern("Dispatcher")), rb_intern("new"), 0);
|
||||
|
||||
if (dispatcher == Qnil) {
|
||||
uwsgi_log("OOOPS\n");
|
||||
exit(1);
|
||||
}
|
||||
else {
|
||||
uwsgi_log("OK %p\n", dispatcher);
|
||||
}
|
||||
|
||||
rb_gc_register_address(&dispatcher);
|
||||
|
||||
call = rb_intern("call");
|
||||
rb_gc_register_address(&call);
|
||||
|
||||
|
||||
rb_uwsgi_io_class = rb_define_class("Uwsgi_IO", rb_cObject);
|
||||
|
||||
rb_gc_register_address(&rb_uwsgi_io_class);
|
||||
|
||||
rb_define_singleton_method(rb_uwsgi_io_class, "new", rb_uwsgi_io_new, 1);
|
||||
rb_define_method(rb_uwsgi_io_class, "initialize", rb_uwsgi_io_init, -1);
|
||||
rb_define_method(rb_uwsgi_io_class, "gets", rb_uwsgi_io_gets, 0);
|
||||
rb_define_method(rb_uwsgi_io_class, "each", rb_uwsgi_io_each, 0);
|
||||
rb_define_method(rb_uwsgi_io_class, "read", rb_uwsgi_io_read, -2);
|
||||
rb_define_method(rb_uwsgi_io_class, "rewind", rb_uwsgi_io_rewind, 0);
|
||||
|
||||
rb_gc_disable();
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
VALUE call_dispatch(VALUE env) {
|
||||
|
||||
return rb_funcall(dispatcher, call, 1, env);
|
||||
|
||||
}
|
||||
|
||||
VALUE send_body(VALUE obj, VALUE fd) {
|
||||
|
||||
size_t len;
|
||||
|
||||
uwsgi_log("sending body\n");
|
||||
if (TYPE(obj) == T_STRING) {
|
||||
uwsgi_log("chunk is a string\n");
|
||||
len = write( NUM2INT(fd), RSTRING(obj)->ptr, RSTRING(obj)->len);
|
||||
}
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
VALUE send_header(VALUE obj, VALUE fd) {
|
||||
|
||||
size_t len;
|
||||
|
||||
uwsgi_log("SENDING HEADER: %d\n", TYPE(obj));
|
||||
|
||||
if (TYPE(obj) == T_ARRAY) {
|
||||
|
||||
if (RARRAY(obj)->len == 2) {
|
||||
VALUE hkey = rb_obj_as_string( RARRAY(obj)->ptr[0]);
|
||||
VALUE hval = rb_obj_as_string( RARRAY(obj)->ptr[1]);
|
||||
|
||||
len = write( NUM2INT(fd), RSTRING(hkey)->ptr, RSTRING(hkey)->len);
|
||||
len = write( NUM2INT(fd), ": ", 2);
|
||||
|
||||
len = write( NUM2INT(fd), RSTRING(hval)->ptr, RSTRING(hval)->len);
|
||||
len = write( NUM2INT(fd), "\r\n", 2);
|
||||
}
|
||||
}
|
||||
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
static void uwsgi_ruby_exception(void) {
|
||||
|
||||
VALUE lasterr = rb_gv_get("$!");
|
||||
VALUE message = rb_obj_as_string(lasterr);
|
||||
|
||||
uwsgi_log("%s\n", RSTRING(message)->ptr);
|
||||
if(!NIL_P(ruby_errinfo)) {
|
||||
VALUE ary = rb_funcall(ruby_errinfo, rb_intern("backtrace"), 0);
|
||||
int i;
|
||||
for (i=0; i<RARRAY(ary)->len; i++) {
|
||||
uwsgi_log("%s\n", RSTRING(RARRAY(ary)->ptr[i])->ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int uwsgi_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int error;
|
||||
int i;
|
||||
|
||||
struct http_status_codes *http_sc;
|
||||
|
||||
/* Standard RACK request */
|
||||
if (!wsgi_req->uh.pktsize) {
|
||||
uwsgi_log("Invalid RACK request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (uwsgi_parse_vars(uwsgi, wsgi_req)) {
|
||||
uwsgi_log("Invalid RACK request. skip.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
VALUE env = rb_hash_new();
|
||||
|
||||
// fill ruby hash
|
||||
for(i=0;i<wsgi_req->var_cnt;i++) {
|
||||
|
||||
rb_hash_aset(env, rb_str_new(wsgi_req->hvec[i].iov_base, wsgi_req->hvec[i].iov_len),
|
||||
rb_str_new(wsgi_req->hvec[i+1].iov_base, wsgi_req->hvec[i+1].iov_len));
|
||||
i++;
|
||||
}
|
||||
|
||||
VALUE rbv = rb_ary_new();
|
||||
rb_ary_store(rbv, 0, INT2NUM(1));
|
||||
rb_ary_store(rbv, 1, INT2NUM(1));
|
||||
rb_hash_aset(env, rb_str_new2("rack.version"), rbv);
|
||||
|
||||
rb_hash_aset(env, rb_str_new2("rack.url_scheme"), rb_str_new2("http"));
|
||||
|
||||
rb_hash_aset(env, rb_str_new2("rack.multithread"), Qfalse);
|
||||
rb_hash_aset(env, rb_str_new2("rack.multiprocess"), Qtrue);
|
||||
rb_hash_aset(env, rb_str_new2("rack.run_once"), Qfalse);
|
||||
|
||||
VALUE dws_wr = Data_Wrap_Struct(rb_uwsgi_io_class, 0, 0, wsgi_req);
|
||||
rb_hash_aset(env, rb_str_new2("rack.input"), rb_funcall(rb_uwsgi_io_class, rb_intern("new"), 1, dws_wr ));
|
||||
|
||||
|
||||
VALUE ret = rb_protect( call_dispatch, env, &error);
|
||||
|
||||
if (error) {
|
||||
uwsgi_ruby_exception();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (TYPE(ret) == T_ARRAY) {
|
||||
if (RARRAY(ret)->len != 3) {
|
||||
uwsgi_log("Invalid RACK response size: %d\n", RARRAY(ret)->len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// manage Status
|
||||
|
||||
|
||||
VALUE status = rb_obj_as_string(RARRAY(ret)->ptr[0]);
|
||||
uwsgi_log("Status: %.*s\n", RSTRING(status)->len, RSTRING(status)->ptr);
|
||||
// get the status code
|
||||
|
||||
wsgi_req->hvec[0].iov_base = "HTTP/1.1 ";
|
||||
wsgi_req->hvec[0].iov_len = 9 ;
|
||||
|
||||
wsgi_req->hvec[1].iov_base = RSTRING(status)->ptr;
|
||||
wsgi_req->hvec[1].iov_len = 3 ;
|
||||
|
||||
wsgi_req->hvec[2].iov_base = " ";
|
||||
wsgi_req->hvec[2].iov_len = 1 ;
|
||||
|
||||
wsgi_req->hvec[3].iov_len = 0 ;
|
||||
|
||||
for (http_sc = hsc; http_sc->message != NULL; http_sc++) {
|
||||
if (!strncmp(http_sc->key, RSTRING(status)->ptr, 3)) {
|
||||
wsgi_req->hvec[3].iov_base = http_sc->message ;
|
||||
wsgi_req->hvec[3].iov_len = http_sc->message_size ;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
wsgi_req->hvec[4].iov_base = "\r\n";
|
||||
wsgi_req->hvec[4].iov_len = 2 ;
|
||||
|
||||
if ( (wsgi_req->response_size = writev(wsgi_req->poll.fd, wsgi_req->hvec, 5)) < 0) {
|
||||
uwsgi_error("writev()");
|
||||
}
|
||||
|
||||
VALUE headers = RARRAY(ret)->ptr[1] ;
|
||||
if (rb_respond_to( headers, rb_intern("each") )) {
|
||||
|
||||
uwsgi_log("headers respond_to each\n");
|
||||
rb_iterate( rb_each, headers, send_header, INT2NUM(wsgi_req->poll.fd));
|
||||
|
||||
}
|
||||
|
||||
uwsgi_log("wsgi_req ptr: %p\n", wsgi_req);
|
||||
|
||||
write(wsgi_req->poll.fd, "\r\n", 2);
|
||||
|
||||
uwsgi_log("PTR: %p\n", RARRAY(ret)->ptr[2]);
|
||||
|
||||
VALUE body = RARRAY(ret)->ptr[2] ;
|
||||
|
||||
//rb_gc_register_address(&body);
|
||||
|
||||
if (rb_respond_to( body, rb_intern("to_path") )) {
|
||||
uwsgi_log("BODY respond_to 'to_path'\n");
|
||||
}
|
||||
else if (rb_respond_to( body, rb_intern("each") )) {
|
||||
rb_iterate( rb_each, body, send_body, INT2NUM(wsgi_req->poll.fd));
|
||||
}
|
||||
|
||||
uwsgi_log("Fatto %p\n", body);
|
||||
|
||||
if (rb_respond_to( body, rb_intern("close") )) {
|
||||
uwsgi_log("BODY respond_to 'close'\n");
|
||||
}
|
||||
|
||||
uwsgi_log("Fatto 2 %p\n", wsgi_req);
|
||||
}
|
||||
|
||||
rb_gc_disable();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void uwsgi_after_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
if (uwsgi->shared->options[UWSGI_OPTION_LOGGING])
|
||||
log_request(wsgi_req);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import os,sys
|
||||
|
||||
NAME='rack'
|
||||
CFLAGS = '-I' + os.popen("ruby -e \"require 'mkmf';print Config::CONFIG['archdir']\"").read().rstrip()
|
||||
LDFLAGS = '-l' + os.popen("ruby -e \"require 'mkmf';print CONFIG['RUBY_SO_NAME']\"").read().rstrip()
|
||||
+453
-162
@@ -2,208 +2,499 @@
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modifier2, char *message, int size, int timeout) {
|
||||
|
||||
int uwsgi_fd;
|
||||
struct sockaddr_in uws_addr;
|
||||
int cnt ;
|
||||
struct uwsgi_header uh;
|
||||
|
||||
if (size > 0xFFFF) {
|
||||
fprintf(stderr,"invalid object (marshalled) size\n");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
uwsgi_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_fd < 0) {
|
||||
perror("socket()");
|
||||
return -1 ;
|
||||
static size_t get_content_length(char *buf, uint16_t size) {
|
||||
int i;
|
||||
size_t val = 0 ;
|
||||
for(i=0;i<size;i++) {
|
||||
if (buf[i] >= '0' && buf[i] <= '9') {
|
||||
val = (val*10) + (buf[i] - '0') ;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(port);
|
||||
uws_addr.sin_addr.s_addr = inet_addr(host);
|
||||
|
||||
|
||||
if (connect(uwsgi_fd, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in))) {
|
||||
perror("connect()");
|
||||
close(uwsgi_fd);
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
uh.modifier1 = modifier1;
|
||||
uh.pktsize = (uint16_t) size ;
|
||||
uh.modifier2 = modifier2;
|
||||
|
||||
cnt = write(uwsgi_fd, &uh, 4) ;
|
||||
if (cnt != 4) {
|
||||
perror("write()");
|
||||
close(uwsgi_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cnt = write(uwsgi_fd, message, size) ;
|
||||
if (cnt != size) {
|
||||
perror("write()");
|
||||
close(uwsgi_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return uwsgi_fd;
|
||||
return val;
|
||||
}
|
||||
|
||||
PyObject *uwsgi_send_message(char *host, int port, uint8_t modifier1, uint8_t modifier2, char *message, int size, int timeout) {
|
||||
#ifdef UWSGI_UDP
|
||||
ssize_t send_udp_message(uint8_t modifier1, char *host, char *message, uint16_t message_size) {
|
||||
|
||||
struct pollfd uwsgi_mpoll ;
|
||||
struct sockaddr_in uws_addr;
|
||||
int cnt ;
|
||||
struct uwsgi_header uh;
|
||||
char buffer[0xFFFF];
|
||||
int fd ;
|
||||
struct sockaddr_in udp_addr;
|
||||
char *udp_port ;
|
||||
ssize_t ret;
|
||||
char udpbuff[1024];
|
||||
|
||||
if (message_size + 4 > 1024)
|
||||
return -1;
|
||||
|
||||
if (size > 0xFFFF) {
|
||||
fprintf(stderr,"invalid object (marshalled) size\n");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
udp_port = strchr(host, ':');
|
||||
if (udp_port == NULL) {
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
uwsgi_mpoll.events = POLLIN ;
|
||||
udp_port[0] = 0 ;
|
||||
|
||||
uwsgi_mpoll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_mpoll.fd < 0) {
|
||||
perror("socket()");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
memset(&udp_addr, 0, sizeof(struct sockaddr_in));
|
||||
udp_addr.sin_family = AF_INET;
|
||||
udp_addr.sin_port = htons(atoi(udp_port));
|
||||
udp_addr.sin_addr.s_addr = inet_addr(host);
|
||||
|
||||
udpbuff[0] = modifier1 ;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
message_size = uwsgi_swap16(message_size);
|
||||
#endif
|
||||
|
||||
memcpy(udpbuff+1, &message_size, 2);
|
||||
|
||||
udpbuff[3] = 0 ;
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
message_size = uwsgi_swap16(message_size);
|
||||
#endif
|
||||
|
||||
memcpy(udpbuff+4, message, message_size);
|
||||
|
||||
ret = sendto(fd, udpbuff, message_size+4, 0, (struct sockaddr *) &udp_addr, sizeof(udp_addr));
|
||||
if (ret < 0) {
|
||||
uwsgi_error("sendto()");
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
int uwsgi_enqueue_message(char *host, int port, uint8_t modifier1, uint8_t modifier2, char *message, int size, int timeout) {
|
||||
|
||||
struct pollfd uwsgi_poll;
|
||||
struct sockaddr_in uws_addr;
|
||||
int cnt;
|
||||
struct uwsgi_header uh;
|
||||
|
||||
if (!timeout)
|
||||
timeout = 1;
|
||||
|
||||
if (size > 0xFFFF) {
|
||||
uwsgi_log( "invalid object (marshalled) size\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(port);
|
||||
uws_addr.sin_port = htons(port);
|
||||
uws_addr.sin_addr.s_addr = inet_addr(host);
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
|
||||
if (connect(uwsgi_mpoll.fd, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in))) {
|
||||
perror("connect()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uh.modifier1 = modifier1;
|
||||
uh.pktsize = (uint16_t) size ;
|
||||
uh.pktsize = (uint16_t) size;
|
||||
uh.modifier2 = modifier2;
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, &uh, 4) ;
|
||||
cnt = write(uwsgi_poll.fd, &uh, 4);
|
||||
if (cnt != 4) {
|
||||
perror("write()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, message, size) ;
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cnt = write(uwsgi_poll.fd, message, size);
|
||||
if (cnt != size) {
|
||||
perror("write()");
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return uwsgi_poll.fd;
|
||||
}
|
||||
|
||||
PyObject *uwsgi_send_message(const char *host, int port, uint8_t modifier1, uint8_t modifier2, char *message, int size, int timeout) {
|
||||
|
||||
struct pollfd uwsgi_mpoll;
|
||||
struct sockaddr_in uws_addr;
|
||||
int cnt;
|
||||
struct uwsgi_header uh;
|
||||
char buffer[0xFFFF];
|
||||
|
||||
|
||||
|
||||
if (!timeout)
|
||||
timeout = 1;
|
||||
|
||||
if (size > 0xFFFF) {
|
||||
uwsgi_log( "invalid object (marshalled) size\n");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
uwsgi_mpoll.events = POLLIN;
|
||||
|
||||
uwsgi_mpoll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_mpoll.fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(port);
|
||||
uws_addr.sin_addr.s_addr = inet_addr(host);
|
||||
|
||||
UWSGI_SET_BLOCKING;
|
||||
|
||||
if (timed_connect(&uwsgi_mpoll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
if (!uwsgi_parse_response(&uwsgi_mpoll, timeout, &uh, buffer)) {
|
||||
|
||||
uh.modifier1 = modifier1;
|
||||
uh.pktsize = (uint16_t) size;
|
||||
uh.modifier2 = modifier2;
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, &uh, 4);
|
||||
if (cnt != 4) {
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
cnt = write(uwsgi_mpoll.fd, message, size);
|
||||
if (cnt != size) {
|
||||
uwsgi_error("write()");
|
||||
close(uwsgi_mpoll.fd);
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
if (!uwsgi_parse_response(&uwsgi_mpoll, timeout, &uh, buffer)) {
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
UWSGI_UNSET_BLOCKING;
|
||||
|
||||
close(uwsgi_mpoll.fd);
|
||||
|
||||
return PyMarshal_ReadObjectFromString(buffer, uh.pktsize);
|
||||
|
||||
if (uh.modifier1 == UWSGI_MODIFIER_RESPONSE) {
|
||||
if (!uh.modifier2) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
else {
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
}
|
||||
|
||||
return PyMarshal_ReadObjectFromString(buffer, uh.pktsize);
|
||||
}
|
||||
|
||||
int uwsgi_parse_response(struct pollfd * upoll, int timeout, struct uwsgi_header *uh, char *buffer) {
|
||||
int uwsgi_parse_response(struct pollfd *upoll, int timeout, struct uwsgi_header *uh, char *buffer) {
|
||||
int rlen, i;
|
||||
|
||||
if (!timeout)
|
||||
timeout = 1;
|
||||
/* first 4 byte header */
|
||||
rlen = poll(upoll, 1, timeout*1000) ;
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
fprintf(stderr, "timeout. skip request\n");
|
||||
close(upoll->fd);
|
||||
return 0 ;
|
||||
}
|
||||
rlen = read(upoll->fd, uh, 4) ;
|
||||
if (rlen > 0 && rlen < 4) {
|
||||
i = rlen ;
|
||||
while(i < 4) {
|
||||
rlen = poll(upoll, 1, timeout*1000) ;
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
fprintf(stderr, "timeout waiting for header. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break ;
|
||||
}
|
||||
rlen = read(upoll->fd, (char *)(uh)+i, 4-i);
|
||||
if (rlen <= 0) {
|
||||
fprintf(stderr, "broken header. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break ;
|
||||
}
|
||||
i += rlen;
|
||||
}
|
||||
if (i < 4) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (rlen <= 0){
|
||||
fprintf(stderr,"invalid request header size: %d...skip\n", rlen);
|
||||
close(upoll->fd);
|
||||
return 0;
|
||||
}
|
||||
/* big endian ? */
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh->pktsize = uwsgi_swap16(uh->pktsize);
|
||||
#endif
|
||||
|
||||
/* check for max buffer size */
|
||||
if (uh->pktsize > uwsgi.buffer_size) {
|
||||
fprintf(stderr,"invalid request block size: %d...skip\n", uh->pktsize);
|
||||
close(upoll->fd);
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
uwsgi_log( "timeout. skip request\n");
|
||||
close(upoll->fd);
|
||||
return 0;
|
||||
}
|
||||
rlen = read(upoll->fd, uh, 4);
|
||||
if (rlen > 0 && rlen < 4) {
|
||||
i = rlen;
|
||||
while (i < 4) {
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
uwsgi_log( "timeout waiting for header. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break;
|
||||
}
|
||||
rlen = read(upoll->fd, (char *) (uh) + i, 4 - i);
|
||||
if (rlen <= 0) {
|
||||
uwsgi_log( "broken header. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break;
|
||||
}
|
||||
i += rlen;
|
||||
}
|
||||
if (i < 4) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (rlen <= 0) {
|
||||
uwsgi_log( "invalid request header size: %d...skip\n", rlen);
|
||||
close(upoll->fd);
|
||||
return 0;
|
||||
}
|
||||
/* big endian ? */
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh->pktsize = uwsgi_swap16(uh->pktsize);
|
||||
#endif
|
||||
|
||||
//fprintf(stderr,"ready for reading %d bytes\n", wsgi_req.size);
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("uwsgi payload size: %d (0x%X) modifier1: %d modifier2: %d\n", uh->pktsize, uh->pktsize, uh->modifier1, uh->modifier2);
|
||||
#endif
|
||||
|
||||
i = 0 ;
|
||||
while(i < uh->pktsize) {
|
||||
rlen = poll(upoll, 1, timeout*1000) ;
|
||||
if (rlen < 0) {
|
||||
perror("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
fprintf(stderr, "timeout. skip request. (expecting %d bytes, got %d)\n", uh->pktsize, i);
|
||||
close(upoll->fd);
|
||||
break ;
|
||||
}
|
||||
rlen = read(upoll->fd, buffer+i, uh->pktsize-i);
|
||||
if (rlen <= 0) {
|
||||
fprintf(stderr, "broken vars. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break ;
|
||||
}
|
||||
i += rlen ;
|
||||
}
|
||||
/* check for max buffer size */
|
||||
if (uh->pktsize > uwsgi.buffer_size) {
|
||||
uwsgi_log( "invalid request block size: %d...skip\n", uh->pktsize);
|
||||
close(upoll->fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
//uwsgi_log("ready for reading %d bytes\n", wsgi_req.size);
|
||||
|
||||
i = 0;
|
||||
while (i < uh->pktsize) {
|
||||
rlen = poll(upoll, 1, timeout * 1000);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("poll()");
|
||||
exit(1);
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
uwsgi_log( "timeout. skip request. (expecting %d bytes, got %d)\n", uh->pktsize, i);
|
||||
close(upoll->fd);
|
||||
break;
|
||||
}
|
||||
rlen = read(upoll->fd, buffer + i, uh->pktsize - i);
|
||||
if (rlen <= 0) {
|
||||
uwsgi_log( "broken vars. skip request.\n");
|
||||
close(upoll->fd);
|
||||
break;
|
||||
}
|
||||
i += rlen;
|
||||
}
|
||||
|
||||
|
||||
if (i < uh->pktsize) {
|
||||
return 0;
|
||||
}
|
||||
if (i < uh->pktsize) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int uwsgi_parse_vars(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
char *buffer = wsgi_req->buffer;
|
||||
|
||||
char *ptrbuf, *bufferend;
|
||||
|
||||
uint16_t strsize = 0;
|
||||
|
||||
ptrbuf = buffer;
|
||||
bufferend = ptrbuf + wsgi_req->uh.pktsize;
|
||||
|
||||
/* set an HTTP 500 status as default */
|
||||
wsgi_req->status = 500;
|
||||
|
||||
while (ptrbuf < bufferend) {
|
||||
if (ptrbuf + 2 < bufferend) {
|
||||
memcpy(&strsize, ptrbuf, 2);
|
||||
#ifdef __BIG_ENDIAN__
|
||||
strsize = uwsgi_swap16(strsize);
|
||||
#endif
|
||||
/* key cannot be null */
|
||||
if (!strsize) {
|
||||
uwsgi_log( "uwsgi key cannot be null. skip this request.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ptrbuf += 2;
|
||||
if (ptrbuf + strsize < bufferend) {
|
||||
// var key
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_base = ptrbuf;
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_len = strsize;
|
||||
ptrbuf += strsize;
|
||||
// value can be null (even at the end) so use <=
|
||||
if (ptrbuf + 2 <= bufferend) {
|
||||
memcpy(&strsize, ptrbuf, 2);
|
||||
#ifdef __BIG_ENDIAN__
|
||||
strsize = uwsgi_swap16(strsize);
|
||||
#endif
|
||||
ptrbuf += 2;
|
||||
if (ptrbuf + strsize <= bufferend) {
|
||||
//uwsgi_log("uwsgi %.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base, strsize, ptrbuf);
|
||||
if (!strncmp("SCRIPT_NAME", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->script_name = ptrbuf;
|
||||
wsgi_req->script_name_len = strsize;
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("SCRIPT_NAME=%.*s\n", wsgi_req->script_name_len, wsgi_req->script_name);
|
||||
#endif
|
||||
}
|
||||
else if (!strncmp("PATH_INFO", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->path_info = ptrbuf;
|
||||
wsgi_req->path_info_len = strsize;
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("PATH_INFO=%.*s\n", wsgi_req->path_info_len, wsgi_req->path_info);
|
||||
#endif
|
||||
}
|
||||
else if (!strncmp("SERVER_PROTOCOL", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->protocol = ptrbuf;
|
||||
wsgi_req->protocol_len = strsize;
|
||||
}
|
||||
else if (!strncmp("REQUEST_URI", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->uri = ptrbuf;
|
||||
wsgi_req->uri_len = strsize;
|
||||
}
|
||||
else if (!strncmp("QUERY_STRING", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->query_string = ptrbuf;
|
||||
wsgi_req->query_string_len = strsize;
|
||||
}
|
||||
else if (!strncmp("REQUEST_METHOD", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->method = ptrbuf;
|
||||
wsgi_req->method_len = strsize;
|
||||
}
|
||||
else if (!strncmp("REMOTE_ADDR", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->remote_addr = ptrbuf;
|
||||
wsgi_req->remote_addr_len = strsize;
|
||||
}
|
||||
else if (!strncmp("REMOTE_USER", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->remote_user = ptrbuf;
|
||||
wsgi_req->remote_user_len = strsize;
|
||||
}
|
||||
else if (!strncmp("UWSGI_SCHEME", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->scheme = ptrbuf;
|
||||
wsgi_req->scheme_len = strsize;
|
||||
}
|
||||
else if (!strncmp("UWSGI_SCRIPT",wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len )) {
|
||||
wsgi_req->wsgi_script = ptrbuf;
|
||||
wsgi_req->wsgi_script_len = strsize;
|
||||
}
|
||||
else if (!strncmp("UWSGI_MODULE", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->wsgi_module = ptrbuf;
|
||||
wsgi_req->wsgi_module_len = strsize;
|
||||
}
|
||||
else if (!strncmp("UWSGI_CALLABLE", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->wsgi_callable = ptrbuf;
|
||||
wsgi_req->wsgi_callable_len = strsize;
|
||||
}
|
||||
else if (!strncmp("UWSGI_PYHOME", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->pyhome = ptrbuf;
|
||||
wsgi_req->pyhome_len = strsize;
|
||||
}
|
||||
else if (!strncmp("SERVER_NAME", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->host = ptrbuf;
|
||||
wsgi_req->host_len = strsize;
|
||||
}
|
||||
else if (!strncmp("HTTPS", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->https = ptrbuf;
|
||||
wsgi_req->https_len = strsize;
|
||||
}
|
||||
else if (!strncmp("CONTENT_LENGTH", wsgi_req->hvec[wsgi_req->var_cnt].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len)) {
|
||||
wsgi_req->post_cl = get_content_length(ptrbuf, strsize);
|
||||
}
|
||||
|
||||
if (wsgi_req->var_cnt < uwsgi->vec_size - (4 + 1)) {
|
||||
wsgi_req->var_cnt++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "max vec size reached. skip this header.\n");
|
||||
return -1;
|
||||
}
|
||||
// var value
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_base = ptrbuf;
|
||||
wsgi_req->hvec[wsgi_req->var_cnt].iov_len = strsize;
|
||||
//uwsgi_log("%.*s = %.*s\n", wsgi_req->hvec[wsgi_req->var_cnt-1].iov_len, wsgi_req->hvec[wsgi_req->var_cnt-1].iov_base, wsgi_req->hvec[wsgi_req->var_cnt].iov_len, wsgi_req->hvec[wsgi_req->var_cnt].iov_base);
|
||||
if (wsgi_req->var_cnt < uwsgi->vec_size - (4 + 1)) {
|
||||
wsgi_req->var_cnt++;
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "max vec size reached. skip this header.\n");
|
||||
return -1;
|
||||
}
|
||||
ptrbuf += strsize;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uwsgi_ping_node(int node, struct wsgi_request *wsgi_req) {
|
||||
|
||||
|
||||
struct pollfd uwsgi_poll;
|
||||
|
||||
struct uwsgi_cluster_node *ucn = &uwsgi.shared->nodes[node];
|
||||
|
||||
if (ucn->name[0] == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ucn->status == UWSGI_NODE_OK) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &ucn->ucn_addr, sizeof(struct sockaddr_in), uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT])) {
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
wsgi_req->uh.modifier1 = UWSGI_MODIFIER_PING;
|
||||
wsgi_req->uh.pktsize = 0;
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
if (write(uwsgi_poll.fd, wsgi_req, 4) != 4) {
|
||||
uwsgi_error("write()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) wsgi_req, wsgi_req->buffer)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,406 @@
|
||||
#ifdef UWSGI_PROXY
|
||||
|
||||
/*
|
||||
|
||||
uWSGI proxy
|
||||
|
||||
it needs one of this tecnology to work:
|
||||
|
||||
- epoll (linux 2.6)
|
||||
- kqueue (various BSD and Darwin)
|
||||
- /dev/poll (Solaris)
|
||||
|
||||
*/
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
#define UWSGI_PROXY_CONNECTING 1
|
||||
#define UWSGI_PROXY_WAITING 2
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
struct uwsgi_proxy_connection {
|
||||
|
||||
int dest_fd;
|
||||
int status;
|
||||
int retry;
|
||||
int node;
|
||||
};
|
||||
|
||||
static void end_proxy(void) {
|
||||
exit(UWSGI_END_CODE);
|
||||
}
|
||||
|
||||
|
||||
static void reload_proxy(void) {
|
||||
exit(UWSGI_RELOAD_CODE);
|
||||
}
|
||||
|
||||
static void uwsgi_proxy_close(struct uwsgi_proxy_connection *upcs, int fd) {
|
||||
|
||||
|
||||
if (upcs[fd].dest_fd >= 0) {
|
||||
close(upcs[fd].dest_fd);
|
||||
upcs[upcs[fd].dest_fd].dest_fd = -1;
|
||||
upcs[upcs[fd].dest_fd].status = 0;
|
||||
upcs[upcs[fd].dest_fd].retry = 0;
|
||||
if (upcs[upcs[fd].dest_fd].node > -1) {
|
||||
if (uwsgi.shared->nodes[upcs[upcs[fd].dest_fd].node].connections > 0)
|
||||
uwsgi.shared->nodes[upcs[upcs[fd].dest_fd].node].connections--;
|
||||
}
|
||||
}
|
||||
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
upcs[fd].dest_fd = -1;
|
||||
upcs[fd].status = 0;
|
||||
upcs[fd].retry = 0;
|
||||
if (upcs[fd].node > -1) {
|
||||
if (uwsgi.shared->nodes[upcs[fd].node].connections > 0)
|
||||
uwsgi.shared->nodes[upcs[fd].node].connections--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static int uwsgi_proxy_find_next_node(int current_node) {
|
||||
|
||||
int i;
|
||||
|
||||
current_node++;
|
||||
if (current_node >= MAX_CLUSTER_NODES) {
|
||||
current_node = 0;
|
||||
}
|
||||
|
||||
// is it a good node ?
|
||||
if (uwsgi.shared->nodes[current_node].name[0] != 0 && uwsgi.shared->nodes[current_node].status == UWSGI_NODE_OK) {
|
||||
if (uwsgi.shared->nodes[current_node].connections < uwsgi.shared->nodes[current_node].workers)
|
||||
return current_node;
|
||||
}
|
||||
|
||||
// try to find a better one
|
||||
|
||||
for (i = 0; i < MAX_CLUSTER_NODES; i++) {
|
||||
if (uwsgi.shared->nodes[i].name[0] != 0 && uwsgi.shared->nodes[i].status == UWSGI_NODE_OK) {
|
||||
if (uwsgi.shared->nodes[i].connections < uwsgi.shared->nodes[i].workers)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
// ok, it is a very loaded system, fallback to round robin
|
||||
if (uwsgi.shared->nodes[current_node].name[0] != 0 && uwsgi.shared->nodes[current_node].status == UWSGI_NODE_OK) {
|
||||
return current_node;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_CLUSTER_NODES; i++) {
|
||||
if (uwsgi.shared->nodes[i].name[0] != 0 && uwsgi.shared->nodes[i].status == UWSGI_NODE_OK) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_proxy(int proxyfd) {
|
||||
|
||||
int efd ;
|
||||
|
||||
#ifdef __linux__
|
||||
struct epoll_event *eevents;
|
||||
struct epoll_event ev;
|
||||
#elif defined(__sun__)
|
||||
struct pollfd *eevents;
|
||||
struct pollfd ev;
|
||||
#else
|
||||
struct kevent *eevents;
|
||||
struct kevent ev;
|
||||
#endif
|
||||
|
||||
int max_events = 64;
|
||||
int nevents, i;
|
||||
const int nonblocking = 1;
|
||||
const int blocking = 0;
|
||||
|
||||
char buffer[4096];
|
||||
ssize_t rlen;
|
||||
ssize_t wlen;
|
||||
int max_connections = sysconf(_SC_OPEN_MAX);
|
||||
|
||||
int soopt;
|
||||
socklen_t solen = sizeof(int);
|
||||
|
||||
int rc;
|
||||
|
||||
struct uwsgi_proxy_connection *upcs;
|
||||
|
||||
struct sockaddr_in upc_addr;
|
||||
socklen_t upc_len = sizeof(struct sockaddr_in);
|
||||
|
||||
int next_node = -1;
|
||||
|
||||
uwsgi_log( "spawned uWSGI proxy (pid: %d)\n", getpid());
|
||||
|
||||
uwsgi_log( "allocating space for %d concurrent proxy connections\n", max_connections);
|
||||
|
||||
// allocate memory for connections
|
||||
upcs = malloc(sizeof(struct uwsgi_proxy_connection) * max_connections);
|
||||
if (!upcs) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
memset(upcs, 0, sizeof(struct uwsgi_proxy_connection) * max_connections);
|
||||
|
||||
|
||||
efd = async_queue_init(proxyfd);
|
||||
if (efd < 0) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
eevents = malloc(sizeof(struct epoll_event) * max_events);
|
||||
memset(&ev, 0, sizeof(struct epoll_event));
|
||||
#elif defined(__sun)
|
||||
eevents = malloc(sizeof(struct pollfd) * max_events);
|
||||
memset(&ev, 0, sizeof(struct pollfd));
|
||||
#else
|
||||
eevents = malloc(sizeof(struct kevent) * max_events);
|
||||
memset(&ev, 0, sizeof(struct kevent));
|
||||
#endif
|
||||
|
||||
if (!eevents) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
signal(SIGINT, (void *) &end_proxy);
|
||||
signal(SIGTERM, (void *) &reload_proxy);
|
||||
signal(SIGHUP, (void *) &reload_proxy);
|
||||
// and welcome to the loop...
|
||||
|
||||
for (;;) {
|
||||
|
||||
nevents = async_wait(efd, eevents, max_events, -1, 0);
|
||||
if (nevents < 0) {
|
||||
uwsgi_error("epoll_wait()");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (i = 0; i < nevents; i++) {
|
||||
|
||||
|
||||
if (eevents[i].ASYNC_FD == proxyfd) {
|
||||
|
||||
if (eevents[i].ASYNC_IS_IN) {
|
||||
// new connection, accept it
|
||||
ev.ASYNC_FD = accept(proxyfd, (struct sockaddr *) &upc_addr, &upc_len);
|
||||
if (ev.ASYNC_FD < 0) {
|
||||
uwsgi_error("accept()");
|
||||
continue;
|
||||
}
|
||||
upcs[ev.ASYNC_FD].node = -1;
|
||||
|
||||
// now connect to the first worker available
|
||||
|
||||
upcs[ev.ASYNC_FD].dest_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (upcs[ev.ASYNC_FD].dest_fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].node = -1;
|
||||
|
||||
// set nonblocking
|
||||
if (ioctl(upcs[ev.ASYNC_FD].dest_fd, FIONBIO, &nonblocking)) {
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
upcs[ev.ASYNC_FD].status = 0;
|
||||
upcs[ev.ASYNC_FD].retry = 0;
|
||||
next_node = uwsgi_proxy_find_next_node(next_node);
|
||||
if (next_node == -1) {
|
||||
uwsgi_log( "unable to find an available worker in the cluster !\n");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].node = next_node;
|
||||
rc = connect(upcs[ev.ASYNC_FD].dest_fd, (struct sockaddr *) &uwsgi.shared->nodes[next_node].ucn_addr, sizeof(struct sockaddr_in));
|
||||
uwsgi.shared->nodes[next_node].connections++;
|
||||
|
||||
if (!rc) {
|
||||
// connected to worker, put it in the epoll_list
|
||||
|
||||
if (async_add(efd, ev.ASYNC_FD, ASYNC_IN)) {
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].dest_fd = ev.ASYNC_FD;
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].status = 0;
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].retry = 0;
|
||||
|
||||
ev.ASYNC_FD = upcs[ev.ASYNC_FD].dest_fd;
|
||||
|
||||
if (async_add(efd, ev.ASYNC_FD, ASYNC_IN)) {
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
// re-set blocking
|
||||
if (ioctl(upcs[upcs[ev.ASYNC_FD].dest_fd].dest_fd, FIONBIO, &blocking)) {
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
else if (errno == EINPROGRESS) {
|
||||
// the socket is waiting, set status to CONNECTING
|
||||
upcs[ev.ASYNC_FD].status = UWSGI_PROXY_WAITING;
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].dest_fd = ev.ASYNC_FD;
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].status = UWSGI_PROXY_CONNECTING;
|
||||
upcs[upcs[ev.ASYNC_FD].dest_fd].retry = 0;
|
||||
|
||||
ev.ASYNC_FD = upcs[ev.ASYNC_FD].dest_fd;
|
||||
if (async_add(efd, ev.ASYNC_FD, ASYNC_OUT)) {
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// connection failed, retry with the next node ?
|
||||
uwsgi_error("connect()");
|
||||
// close only when all node are tried
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "!!! something horrible happened to the uWSGI proxy, reloading it !!!\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// this is for clients/workers
|
||||
if (eevents[i].ASYNC_IS_IN) {
|
||||
|
||||
// is this a connected client/worker ?
|
||||
//uwsgi_log("ready %d\n", upcs[eevents[i].data.fd].status);
|
||||
|
||||
if (!upcs[eevents[i].ASYNC_FD].status) {
|
||||
if (upcs[eevents[i].ASYNC_FD].dest_fd >= 0) {
|
||||
|
||||
rlen = read(eevents[i].ASYNC_FD, buffer, 4096);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("read()");
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
else if (rlen == 0) {
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
wlen = write(upcs[eevents[i].ASYNC_FD].dest_fd, buffer, rlen);
|
||||
if (wlen != rlen) {
|
||||
uwsgi_error("write()");
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (upcs[eevents[i].ASYNC_FD].status == UWSGI_PROXY_WAITING) {
|
||||
// disconnected node
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "UNKNOWN STATUS %d\n", upcs[eevents[i].ASYNC_FD].status);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (eevents[i].ASYNC_IS_OUT) {
|
||||
if (upcs[eevents[i].ASYNC_FD].status == UWSGI_PROXY_CONNECTING) {
|
||||
|
||||
|
||||
#ifdef UWSGI_PROXY_USE_KQUEUE
|
||||
if (getsockopt(eevents[i].ASYNC_FD, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
uwsgi_error("getsockopt()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt) {
|
||||
uwsgi_log( "connect() %s\n", strerror(soopt));
|
||||
// increase errors on node
|
||||
uwsgi_log( "*** marking cluster node %d/%s as failed ***\n", upcs[eevents[i].ASYNC_FD].node, uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].name);
|
||||
uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].errors++;
|
||||
uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].status = UWSGI_NODE_FAILED;
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
// increase errors on node
|
||||
#endif
|
||||
ev.ASYNC_FD = upcs[eevents[i].ASYNC_FD].dest_fd;
|
||||
upcs[ev.ASYNC_FD].status = 0;
|
||||
if (async_add(efd, ev.ASYNC_FD, ASYNC_IN)) {
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
|
||||
ev.ASYNC_FD = upcs[ev.ASYNC_FD].dest_fd;
|
||||
upcs[ev.ASYNC_FD].status = 0;
|
||||
|
||||
if (async_mod(efd, ev.ASYNC_FD, ASYNC_IN)) {
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
// re-set blocking
|
||||
if (ioctl(ev.ASYNC_FD, FIONBIO, &blocking)) {
|
||||
uwsgi_error("ioctl()");
|
||||
uwsgi_proxy_close(upcs, ev.ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "strange event for %d\n", (int) eevents[i].ASYNC_FD);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (upcs[eevents[i].ASYNC_FD].status == UWSGI_PROXY_CONNECTING) {
|
||||
if (getsockopt(eevents[i].ASYNC_FD, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
uwsgi_error("getsockopt()");
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt) {
|
||||
uwsgi_log( "connect() %s\n", strerror(soopt));
|
||||
}
|
||||
|
||||
// increase errors on node
|
||||
uwsgi_log( "*** marking cluster node %d/%s as failed ***\n", upcs[eevents[i].ASYNC_FD].node, uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].name);
|
||||
uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].errors++;
|
||||
uwsgi.shared->nodes[upcs[eevents[i].ASYNC_FD].node].status = UWSGI_NODE_FAILED;
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "STRANGE EVENT !!! %d %d %d\n", (int) eevents[i].ASYNC_FD, (int) eevents[i].ASYNC_EV, upcs[eevents[i].ASYNC_FD].status);
|
||||
}
|
||||
uwsgi_proxy_close(upcs, eevents[i].ASYNC_FD);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#warning "*** PROXY support is disabled ***"
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
import uwsgi
|
||||
import sys
|
||||
|
||||
if uwsgi.load_plugin(0, "plugins/psgi/psgi_plugin.so", "mojoapp.pl"):
|
||||
print "PSGI plugin loaded"
|
||||
else:
|
||||
print "unable to load PSGI plugin"
|
||||
sys.exit(1)
|
||||
@@ -0,0 +1,255 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
int manage_python_response(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
PyObject *pychunk ;
|
||||
ssize_t wsize ;
|
||||
#ifdef UWSGI_SENDFILE
|
||||
ssize_t sf_len = 0 ;
|
||||
#endif
|
||||
|
||||
// return or yield ?
|
||||
if (PyString_Check((PyObject *)wsgi_req->async_result)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(wsgi_req->async_result), PyString_Size(wsgi_req->async_result))) < 0) {
|
||||
uwsgi_error("write()");
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
goto clear;
|
||||
}
|
||||
|
||||
#ifdef PYTHREE
|
||||
if (PyBytes_Check((PyObject *)wsgi_req->async_result)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyBytes_AsString(wsgi_req->async_result), PyBytes_Size(wsgi_req->async_result))) < 0) {
|
||||
uwsgi_error("write()");
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
goto clear;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
if (wsgi_req->sendfile_obj == wsgi_req->async_result && wsgi_req->sendfile_fd != -1) {
|
||||
sf_len = uwsgi_sendfile(uwsgi, wsgi_req);
|
||||
if (sf_len < 1) goto clear;
|
||||
wsgi_req->response_size += sf_len ;
|
||||
#ifdef UWSGI_ASYNC
|
||||
if (uwsgi->async > 1) {
|
||||
if (wsgi_req->response_size < wsgi_req->sendfile_fd_size) {
|
||||
return UWSGI_AGAIN;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
goto clear;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_WSGI2
|
||||
// is it a tuple (WSGI2.0) ?
|
||||
// move it to another hook !!!
|
||||
if (PyTuple_Check((PyObject *)wsgi_req->async_result)) {
|
||||
if (PyTuple_Size((PyObject *)wsgi_req->async_result) != 3) {
|
||||
uwsgi_log("invalid WSGI2.0 response size: %d.\n", PyTuple_Size((PyObject *)wsgi_req->async_result));
|
||||
goto clear;
|
||||
}
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("wsgi_req->async_result = %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt);
|
||||
#endif
|
||||
if (py_uwsgi_spit(NULL, (PyObject *)wsgi_req->async_result) == Py_None) {
|
||||
goto clear;
|
||||
}
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("wsgi_req->async_result = %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt);
|
||||
#endif
|
||||
wsgi_req->async_orig_result = wsgi_req->async_result ;
|
||||
wsgi_req->async_result = PyTuple_GetItem((PyObject *)wsgi_req->async_result, 2);
|
||||
#ifdef UWSGI_DEBUG
|
||||
uwsgi_debug("wsgi_req->async_orig_result = %d\n", ((PyObject *)wsgi_req->async_orig_result)->ob_refcnt);
|
||||
uwsgi_debug("wsgi_req->async_result = %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt);
|
||||
#endif
|
||||
return UWSGI_AGAIN;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ok its a yield
|
||||
if (!wsgi_req->async_placeholder) {
|
||||
wsgi_req->async_placeholder = PyObject_GetIter(wsgi_req->async_result);
|
||||
if (!wsgi_req->async_placeholder) {
|
||||
goto clear2;
|
||||
}
|
||||
#ifdef UWSGI_ASYNC
|
||||
if (uwsgi->async > 1) {
|
||||
return UWSGI_AGAIN;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
pychunk = PyIter_Next(wsgi_req->async_placeholder) ;
|
||||
|
||||
if (!pychunk) {
|
||||
if (PyErr_Occurred()) PyErr_Print();
|
||||
goto clear;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (PyString_Check(pychunk)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyString_AsString(pychunk), PyString_Size(pychunk))) < 0) {
|
||||
uwsgi_error("write()");
|
||||
Py_DECREF(pychunk);
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
}
|
||||
|
||||
#ifdef PYTHREE
|
||||
else if (PyBytes_Check(pychunk)) {
|
||||
if ((wsize = write(wsgi_req->poll.fd, PyBytes_AsString(pychunk), PyBytes_Size(pychunk))) < 0) {
|
||||
uwsgi_error("write()");
|
||||
Py_DECREF(pychunk);
|
||||
goto clear;
|
||||
}
|
||||
wsgi_req->response_size += wsize;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_SENDFILE
|
||||
else if (wsgi_req->sendfile_obj == pychunk && wsgi_req->sendfile_fd != -1) {
|
||||
sf_len = uwsgi_sendfile(uwsgi, wsgi_req);
|
||||
if (sf_len < 1) goto clear;
|
||||
wsgi_req->response_size += sf_len ;
|
||||
}
|
||||
#endif
|
||||
|
||||
Py_DECREF(pychunk);
|
||||
return UWSGI_AGAIN ;
|
||||
|
||||
clear:
|
||||
if (wsgi_req->sendfile_fd != -1) {
|
||||
Py_DECREF((PyObject *)wsgi_req->async_sendfile);
|
||||
}
|
||||
if (wsgi_req->async_environ) {
|
||||
PyDict_Clear(wsgi_req->async_environ);
|
||||
}
|
||||
if (wsgi_req->async_post && !wsgi_req->fd_closed) {
|
||||
fclose(wsgi_req->async_post);
|
||||
if (!uwsgi->post_buffering || wsgi_req->post_cl <= uwsgi->post_buffering) {
|
||||
wsgi_req->fd_closed = 1 ;
|
||||
|
||||
}
|
||||
}
|
||||
Py_XDECREF((PyObject *)wsgi_req->async_placeholder);
|
||||
#ifdef UWSGI_WSGI2
|
||||
Py_XDECREF((PyObject *)wsgi_req->async_orig_result);
|
||||
#endif
|
||||
clear2:
|
||||
Py_DECREF((PyObject *)wsgi_req->async_result);
|
||||
PyErr_Clear();
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
if (wsgi_req->async_placeholder) {
|
||||
uwsgi_debug("wsgi_req->async_placeholder: %d\n", ((PyObject *)wsgi_req->async_placeholder)->ob_refcnt);
|
||||
}
|
||||
#ifdef UWSGI_WSGI2
|
||||
if (wsgi_req->async_orig_result) {
|
||||
uwsgi_debug("wsgi_req->async_orig_result: %d\n", ((PyObject *)wsgi_req->async_orig_result)->ob_refcnt);
|
||||
}
|
||||
#endif
|
||||
if (wsgi_req->async_result) {
|
||||
uwsgi_debug("wsgi_req->async_result: %d\n", ((PyObject *)wsgi_req->async_result)->ob_refcnt);
|
||||
}
|
||||
if (wsgi_req->async_app) {
|
||||
uwsgi_debug("wsgi_req->async_app: %d\n", ((PyObject *)wsgi_req->async_app)->ob_refcnt);
|
||||
}
|
||||
#endif
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
|
||||
PyObject *python_call(PyObject *callable, PyObject *args, int catch) {
|
||||
|
||||
PyObject *pyret;
|
||||
|
||||
pyret = PyEval_CallObject(callable, args);
|
||||
if (PyErr_Occurred()) {
|
||||
if (!catch) {
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef UWSGI_DEBUG
|
||||
if (pyret) {
|
||||
uwsgi_debug("called %p %p %d\n", callable, args, pyret->ob_refcnt);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
return pyret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int uwsgi_python_call(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req, PyObject *callable, PyObject *args) {
|
||||
|
||||
wsgi_req->async_result = python_call(callable, args, 0);
|
||||
|
||||
if (wsgi_req->async_result) {
|
||||
while ( manage_python_response(uwsgi, wsgi_req) != UWSGI_OK) {
|
||||
#ifdef UWSGI_ASYNC
|
||||
if (uwsgi->async > 1) {
|
||||
return UWSGI_AGAIN;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return UWSGI_OK;
|
||||
}
|
||||
|
||||
void init_pyargv(struct uwsgi_server *uwsgi) {
|
||||
|
||||
#ifdef PYTHREE
|
||||
wchar_t pname[6];
|
||||
mbstowcs(pname, "uwsgi", 6);
|
||||
uwsgi->py_argv[0] = pname;
|
||||
#else
|
||||
uwsgi->py_argv[0] = "uwsgi";
|
||||
#endif
|
||||
|
||||
if (uwsgi->pyargv != NULL && !uwsgi->pyargc) {
|
||||
uwsgi->pyargc++;
|
||||
#ifdef PYTHREE
|
||||
wchar_t *wcargv = malloc( sizeof( wchar_t ) * (strlen(uwsgi->pyargv)+1));
|
||||
if (!wcargv) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
memset(wcargv, 0, sizeof( wchar_t ) * (strlen(uwsgi->pyargv)+1));
|
||||
#endif
|
||||
char *ap;
|
||||
#ifdef __sun__
|
||||
// FIX THIS !!!
|
||||
ap = strtok(uwsgi->pyargv, " ");
|
||||
while ((ap = strtok(NULL, " ")) != NULL) {
|
||||
#else
|
||||
while ((ap = strsep(&uwsgi->pyargv, " \t")) != NULL) {
|
||||
#endif
|
||||
if (*ap != '\0') {
|
||||
#ifdef PYTHREE
|
||||
mbstowcs( wcargv + strlen(ap), ap, strlen(ap));
|
||||
uwsgi->py_argv[uwsgi->pyargc] = wcargv + strlen(ap);
|
||||
#else
|
||||
uwsgi->py_argv[uwsgi->pyargc] = ap;
|
||||
#endif
|
||||
uwsgi->pyargc++;
|
||||
}
|
||||
if (uwsgi->pyargc + 1 > MAX_PYARGV)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PySys_SetArgv(uwsgi->pyargc, uwsgi->py_argv);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
#ifdef UWSGI_ROUTING
|
||||
#include "uwsgi.h"
|
||||
|
||||
void routing_setup(struct uwsgi_server *uwsgi) {
|
||||
|
||||
int i;
|
||||
struct uwsgi_route *ur;
|
||||
int max_ovec = 0 ;
|
||||
|
||||
for(i=0;i<uwsgi->nroutes;i++) {
|
||||
uwsgi_log("%d = %p\n", i, uwsgi->routes[i].pattern);
|
||||
ur = &uwsgi->routes[i];
|
||||
if (ur->args > max_ovec) {
|
||||
max_ovec = ur->args;
|
||||
}
|
||||
}
|
||||
|
||||
uwsgi->async_ovector = malloc( sizeof(int *) * uwsgi->async);
|
||||
if (!uwsgi->async_ovector) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi_log("max_ovec = %d\n", max_ovec);
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
uwsgi->async_ovector[i] = malloc(sizeof(int) * ((max_ovec+1)*3));
|
||||
if (!uwsgi->async_ovector[i]) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void check_route(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int ret,i ;
|
||||
struct uwsgi_route *ur;
|
||||
|
||||
for(i=0;i<uwsgi->nroutes;i++) {
|
||||
|
||||
//uwsgi_log("checking route %d\n", i);
|
||||
ur = &uwsgi->routes[i];
|
||||
ret = pcre_exec(ur->pattern, ur->pattern_extra, wsgi_req->path_info, wsgi_req->path_info_len, 0, 0, wsgi_req->ovector, (ur->args+1)*3 );
|
||||
|
||||
if (ret >= 0) {
|
||||
uwsgi_log("found route %d for PATH_INFO=%.*s\n", i, wsgi_req->path_info_len, wsgi_req->path_info);
|
||||
if (ur->action) {
|
||||
ur->action(uwsgi, wsgi_req, ur);
|
||||
}
|
||||
else {
|
||||
uwsgi_route_action_wsgi(uwsgi, wsgi_req, ur);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO check for errors if < 0 && != NO_MATCH */
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
void uwsgi_route_action_wsgi(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
|
||||
|
||||
int i;
|
||||
PyObject *route_py_callbase, *route_py_dict;
|
||||
|
||||
uwsgi_log("managing WSGI route...\n");
|
||||
if (ur->callable == NULL) {
|
||||
if (ur->callbase) {
|
||||
route_py_callbase = PyImport_ImportModule(ur->callbase);
|
||||
if (route_py_callbase == NULL) {
|
||||
PyErr_Print();
|
||||
}
|
||||
else {
|
||||
uwsgi_log("callbase dict ok for %s\n", ur->call);
|
||||
route_py_dict = PyModule_GetDict(route_py_callbase);
|
||||
}
|
||||
}
|
||||
|
||||
ur->callable = PyDict_GetItemString(route_py_dict, ur->call);
|
||||
if (ur->callable == NULL) {
|
||||
uwsgi_log("route_py_dict: %p call: %s\n", route_py_dict, ur->call);
|
||||
PyErr_Print();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO put regex captured groups in WSGI env
|
||||
|
||||
|
||||
PyObject *ra = PyTuple_New(ur->args);
|
||||
for (i=1;i<=ur->args;i++) {
|
||||
PyTuple_SetItem(ra, i-1, PyString_FromStringAndSize(
|
||||
wsgi_req->path_info + wsgi_req->ovector[i*2],
|
||||
wsgi_req->ovector[(i*2)+1] - wsgi_req->ovector[i*2]
|
||||
));
|
||||
}
|
||||
|
||||
PyDict_SetItemString(wsgi_req->async_environ, "x-wsgiorg.uwsgi.route_args", ra);
|
||||
|
||||
if (ur->callable) {
|
||||
wsgi_req->async_app = ur->callable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void uwsgi_route_action_uwsgi(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
|
||||
|
||||
PyObject *route_py_callbase, *route_py_dict;
|
||||
int i;
|
||||
|
||||
uwsgi_log("managing uwsgi route...\n");
|
||||
if (ur->callable == NULL) {
|
||||
if (ur->callbase) {
|
||||
route_py_callbase = PyImport_ImportModule(ur->callbase);
|
||||
if (route_py_callbase == NULL) {
|
||||
PyErr_Print();
|
||||
}
|
||||
else {
|
||||
uwsgi_log("callbase dict ok for %s\n", ur->call);
|
||||
route_py_dict = PyModule_GetDict(route_py_callbase);
|
||||
}
|
||||
}
|
||||
|
||||
ur->callable = PyDict_GetItemString(route_py_dict, ur->call);
|
||||
if (ur->callable == NULL) {
|
||||
uwsgi_log("route_py_dict: %p call: %s\n", route_py_dict, ur->call);
|
||||
PyErr_Print();
|
||||
}
|
||||
|
||||
ur->callable_args = PyTuple_New(ur->args+1);
|
||||
}
|
||||
|
||||
|
||||
if (ur->callable) {
|
||||
uwsgi_log("route callable dict ok: %d\n", ur->args);
|
||||
wsgi_req->async_app = ur->callable;
|
||||
wsgi_req->async_args = ur->callable_args;
|
||||
|
||||
for (i=1;i<=ur->args;i++) {
|
||||
uwsgi_log("%d\n", i);
|
||||
uwsgi_log("%d / %d\n", wsgi_req->ovector[i*2], wsgi_req->ovector[(i*2)+1]);
|
||||
uwsgi_log("%d = %.*s\n", i,wsgi_req->ovector[(i*2)+1] - wsgi_req->ovector[i*2],
|
||||
wsgi_req->path_info + wsgi_req->ovector[i*2]);
|
||||
PyTuple_SetItem(wsgi_req->async_args, i, PyString_FromStringAndSize(
|
||||
wsgi_req->path_info + wsgi_req->ovector[i*2],
|
||||
wsgi_req->ovector[(i*2)+1] - wsgi_req->ovector[i*2]
|
||||
));
|
||||
}
|
||||
|
||||
uwsgi_log("route callable built\n");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
#ifdef UWSGI_SENDFILE
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
PyObject *py_uwsgi_sendfile(PyObject * self, PyObject * args) {
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O|i:uwsgi_sendfile", &wsgi_req->async_sendfile, &wsgi_req->sendfile_fd_chunk)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef PYTHREE
|
||||
wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile);
|
||||
#else
|
||||
if (PyFile_Check((PyObject *)wsgi_req->async_sendfile)) {
|
||||
Py_INCREF((PyObject *)wsgi_req->async_sendfile);
|
||||
wsgi_req->sendfile_fd = PyObject_AsFileDescriptor(wsgi_req->async_sendfile);
|
||||
}
|
||||
#endif
|
||||
|
||||
// PEP 333 hack
|
||||
wsgi_req->sendfile_obj = wsgi_req->async_sendfile;
|
||||
//wsgi_req->sendfile_obj = (void *) PyTuple_New(0);
|
||||
|
||||
Py_INCREF((PyObject *) wsgi_req->sendfile_obj);
|
||||
return (PyObject *) wsgi_req->sendfile_obj;
|
||||
}
|
||||
|
||||
ssize_t uwsgi_sendfile(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int fd = wsgi_req->sendfile_fd ;
|
||||
int sockfd = wsgi_req->poll.fd ;
|
||||
struct stat stat_buf;
|
||||
|
||||
if (!wsgi_req->sendfile_fd_size) {
|
||||
|
||||
if (fstat(fd, &stat_buf)) {
|
||||
uwsgi_error("fstat()");
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
wsgi_req->sendfile_fd_size = stat_buf.st_size;
|
||||
}
|
||||
}
|
||||
|
||||
if (wsgi_req->sendfile_fd_size) {
|
||||
|
||||
if (!wsgi_req->sendfile_fd_chunk) wsgi_req->sendfile_fd_chunk = 4096 ;
|
||||
|
||||
return uwsgi_do_sendfile(sockfd, wsgi_req->sendfile_fd, wsgi_req->sendfile_fd_size, wsgi_req->sendfile_fd_chunk, &wsgi_req->sendfile_fd_pos, uwsgi->async);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
ssize_t uwsgi_do_sendfile(int sockfd, int filefd, size_t filesize, size_t chunk, off_t *pos, int async) {
|
||||
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
||||
|
||||
int sf_ret;
|
||||
|
||||
off_t sf_len = filesize ;
|
||||
|
||||
if (async > 1) {
|
||||
sf_len = chunk;
|
||||
sf_ret = sendfile(filefd, sockfd, *pos, 0, NULL, &sf_len, 0);
|
||||
*pos += sf_len ;
|
||||
}
|
||||
else {
|
||||
sf_ret = sendfile(filefd, sockfd, 0, 0, NULL, &sf_len, 0);
|
||||
}
|
||||
|
||||
if (sf_ret) {
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return sf_len;
|
||||
#elif defined(__APPLE__)
|
||||
int sf_ret;
|
||||
off_t sf_len = filesize ;
|
||||
|
||||
if (async > 1) {
|
||||
sf_len = chunk;
|
||||
sf_ret = sendfile(filefd, sockfd, *pos, &sf_len, NULL, 0);
|
||||
*pos += sf_len ;
|
||||
}
|
||||
else {
|
||||
sf_ret = sendfile(filefd, sockfd, 0, &sf_len, NULL, 0);
|
||||
}
|
||||
|
||||
if (sf_ret) {
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return sf_len;
|
||||
|
||||
#elif defined(__linux__) || defined(__sun__)
|
||||
int sf_ret;
|
||||
|
||||
if (async > 1) {
|
||||
sf_ret = sendfile(sockfd, filefd, pos, chunk);
|
||||
}
|
||||
else {
|
||||
sf_ret = sendfile(sockfd, filefd, pos, filesize);
|
||||
}
|
||||
|
||||
if (sf_ret < 0) {
|
||||
uwsgi_error("sendfile()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return sf_ret ;
|
||||
#else
|
||||
static size_t nosf_buf_size = 0 ;
|
||||
static char *nosf_buf ;
|
||||
|
||||
ssize_t jlen = 0;
|
||||
ssize_t rlen = 0;
|
||||
ssize_t i = 0;
|
||||
|
||||
if (!nosf_buf) {
|
||||
nosf_buf = malloc(chunk);
|
||||
}
|
||||
else if (chunk != nosf_buf_size) {
|
||||
nosf_buf = realloc(nosf_buf, chunk);
|
||||
}
|
||||
|
||||
nosf_buf_size = chunk ;
|
||||
|
||||
if (async > 1) {
|
||||
jlen = read(filefd, nosf_buf, chunk);
|
||||
if (jlen <= 0) {
|
||||
uwsgi_error("read()");
|
||||
return 0;
|
||||
}
|
||||
jlen = write(sockfd, nosf_buf, jlen);
|
||||
if (jlen <= 0) {
|
||||
uwsgi_error("write()");
|
||||
return 0;
|
||||
}
|
||||
return jlen ;
|
||||
}
|
||||
|
||||
while (i < filesize) {
|
||||
jlen = read(filefd, nosf_buf, chunk);
|
||||
if (jlen <= 0) {
|
||||
uwsgi_error("read()");
|
||||
break;
|
||||
}
|
||||
i += jlen;
|
||||
jlen = write(sockfd, nosf_buf, jlen);
|
||||
if (jlen <= 0) {
|
||||
uwsgi_error("write()");
|
||||
break;
|
||||
}
|
||||
rlen += jlen ;
|
||||
}
|
||||
|
||||
return rlen;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
import sys
|
||||
import uwsgiconfig as uc
|
||||
import shutil
|
||||
|
||||
from setuptools import setup
|
||||
from setuptools.dist import Distribution
|
||||
from setuptools.command.install import install
|
||||
from setuptools.command.build_ext import build_ext
|
||||
|
||||
class uWSGIBuilder(build_ext):
|
||||
|
||||
def run(self):
|
||||
uc.parse_vars()
|
||||
uc.build_uwsgi(sys.prefix + '/bin/' + uc.UWSGI_BIN_NAME)
|
||||
|
||||
|
||||
class uWSGIInstall(install):
|
||||
|
||||
def run(self):
|
||||
# hack, hack and still hack. We need to find a solution for 0.9.6
|
||||
if self.record:
|
||||
record_file = open(self.record,'w')
|
||||
uc.parse_vars()
|
||||
uc.build_uwsgi(sys.prefix + '/bin/' + uc.UWSGI_BIN_NAME)
|
||||
|
||||
class uWSGIDistribution(Distribution):
|
||||
|
||||
def __init__(self, *attrs):
|
||||
Distribution.__init__(self, *attrs)
|
||||
self.cmdclass['install'] = uWSGIInstall
|
||||
self.cmdclass['build_ext'] = uWSGIBuilder
|
||||
|
||||
|
||||
|
||||
setup(name='uWSGI',
|
||||
version='0.9.6-dev',
|
||||
description='The uWSGI server',
|
||||
author='Unbit',
|
||||
author_email='info@unbit.it',
|
||||
url='http://projects.unbit.it/uwsgi/',
|
||||
license='GPL2',
|
||||
distclass = uWSGIDistribution,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import uwsgi
|
||||
|
||||
print "uWSGI version:", uwsgi.version
|
||||
|
||||
def ciao():
|
||||
print "modifica su /tmp"
|
||||
|
||||
def ciao2():
|
||||
print "nuovo uwsgi_server"
|
||||
|
||||
counter = 0
|
||||
|
||||
#if uwsgi.load_plugin(0, 'plugins/example/example_plugin.so', 'ciao'):
|
||||
# print "example plugin loaded"
|
||||
#else:
|
||||
# print "unable to load example plugin"
|
||||
|
||||
#uwsgi.event_add(uwsgi.EVENT_FILE, "/tmp", ciao)
|
||||
#uwsgi.event_add(uwsgi.EVENT_DNSSD, "_uwsgi._tcp", ciao2)
|
||||
#uwsgi.event_add(uwsgi.EVENT_TIMER, 1000, ciao2)
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
global counter
|
||||
|
||||
print env
|
||||
start_response('200 Ok', [('Content-type', 'text/plain')])
|
||||
yield "hello world"
|
||||
yield "hello world2"
|
||||
|
||||
for i in xrange(1,1000):
|
||||
yield str(i)
|
||||
|
||||
yield "\n"
|
||||
|
||||
yield str(counter)
|
||||
counter += 1
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
def mygen(uri):
|
||||
for i in xrange(1,100):
|
||||
yield "ciao %s<br/>" % uri
|
||||
|
||||
|
||||
def application(env, start_response = None):
|
||||
return '200 OK', [('Content-Type', 'text/html')], "<h1>This is the fastest homepage of the world !!!</h1>"
|
||||
#return '200 OK', [('Content-Type', 'text/html')], mygen(env['PATH_INFO'])
|
||||
@@ -0,0 +1,6 @@
|
||||
import uwsgi
|
||||
|
||||
def print_logs(ip, port, message):
|
||||
print(ip, port, message)
|
||||
|
||||
uwsgi.udp_callable = print_logs
|
||||
@@ -0,0 +1,455 @@
|
||||
#ifdef UWSGI_SNMP
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
#define SNMP_SEQUENCE 0x30
|
||||
#define SNMP_INTEGER 0x02
|
||||
#define SNMP_STRING 0x04
|
||||
#define SNMP_NULL 0x05
|
||||
#define SNMP_GET 0xA0
|
||||
#define SNMP_RES 0xA2
|
||||
#define SNMP_OID 0x06
|
||||
|
||||
#define SNMP_WATERMARK (127-8)
|
||||
|
||||
/* 1.3.6.1.4.1.35156.17.X.X */
|
||||
#define SNMP_UWSGI_BASE "\x2B\x06\x01\x04\x01\x82\x92\x54\x11"
|
||||
|
||||
static int get_snmp_integer(uint8_t *, uint64_t *);
|
||||
|
||||
static uint64_t get_uwsgi_snmp_value(uint64_t, uint8_t *);
|
||||
static uint64_t get_uwsgi_custom_snmp_value(uint64_t, uint8_t *);
|
||||
|
||||
static uint8_t snmp_int_to_snmp(uint64_t, uint8_t, uint8_t *);
|
||||
|
||||
static ssize_t build_snmp_response(uint8_t, uint8_t, uint8_t *, int, uint8_t *, uint8_t *, uint8_t *);
|
||||
|
||||
void manage_snmp(int fd, uint8_t * buffer, int size, struct sockaddr_in *client_addr) {
|
||||
|
||||
uint16_t asnlen;
|
||||
uint16_t oidlen;
|
||||
|
||||
uint8_t oid_part[2];
|
||||
|
||||
int ptrdelta;
|
||||
|
||||
uint8_t *ptr = buffer, *seq1, *seq2, *seq3;
|
||||
|
||||
uint8_t community_len;
|
||||
|
||||
uint64_t snmp_int;
|
||||
uint64_t request_id;
|
||||
uint64_t version;
|
||||
|
||||
|
||||
// KISS for memory management
|
||||
if (size > SNMP_WATERMARK)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
// check total sequence size
|
||||
if (*ptr > SNMP_WATERMARK || *ptr < 13)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
|
||||
|
||||
// check snmp version
|
||||
if (*ptr != SNMP_INTEGER)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
ptrdelta = get_snmp_integer(ptr, &version);
|
||||
if (version > 2)
|
||||
return;
|
||||
ptr += ptrdelta;
|
||||
|
||||
|
||||
|
||||
|
||||
// check for community string (this must be set from the python vm using uwsgi.snmp_community or with --snmp-community arg)
|
||||
if (*ptr != SNMP_STRING)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
community_len = *ptr;
|
||||
|
||||
|
||||
|
||||
if (community_len > 72 || community_len < 1)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
// check for community string
|
||||
if (strlen(uwsgi.shared->snmp_community) != community_len)
|
||||
return;
|
||||
if (memcmp(ptr, uwsgi.shared->snmp_community, community_len))
|
||||
return;
|
||||
|
||||
ptr += community_len;
|
||||
|
||||
// check for get request
|
||||
if (*ptr != SNMP_GET)
|
||||
return;
|
||||
*ptr = SNMP_RES;
|
||||
ptr++;
|
||||
seq1 = ptr;
|
||||
|
||||
|
||||
if (*ptr != ((size - community_len) - 9))
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
|
||||
// get request_id
|
||||
if (*ptr != SNMP_INTEGER)
|
||||
return;
|
||||
ptr++;
|
||||
ptrdelta = get_snmp_integer(ptr, &request_id);
|
||||
|
||||
|
||||
if (ptrdelta <= 0)
|
||||
return;
|
||||
|
||||
// check here
|
||||
if (ptr + ptrdelta >= buffer + size)
|
||||
return;
|
||||
ptr += ptrdelta;
|
||||
|
||||
// get error
|
||||
if (*ptr != SNMP_INTEGER)
|
||||
return;
|
||||
ptr++;
|
||||
ptrdelta = get_snmp_integer(ptr, &snmp_int);
|
||||
if (ptrdelta <= 0)
|
||||
return;
|
||||
if (ptr + ptrdelta >= buffer + size)
|
||||
return;
|
||||
if (snmp_int != 0)
|
||||
return;
|
||||
ptr += ptrdelta;
|
||||
|
||||
// get index
|
||||
if (*ptr != SNMP_INTEGER)
|
||||
return;
|
||||
ptr++;
|
||||
ptrdelta = get_snmp_integer(ptr, &snmp_int);
|
||||
if (ptrdelta <= 0)
|
||||
return;
|
||||
if (ptr + ptrdelta >= buffer + size)
|
||||
return;
|
||||
|
||||
if (snmp_int != 0)
|
||||
return;
|
||||
ptr += ptrdelta;
|
||||
|
||||
// check for sequence
|
||||
if (*ptr != SNMP_SEQUENCE)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
if (*ptr > SNMP_WATERMARK)
|
||||
return;
|
||||
seq2 = ptr;
|
||||
ptr++;
|
||||
|
||||
// now the interesting stuff: OID management
|
||||
if (*ptr != SNMP_SEQUENCE)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
|
||||
// check for normal OID uWSGI size: |1.3|.6|.1|.4|.1.|35156|.17|.1/2|.x| + OID_NULL
|
||||
asnlen = *ptr;
|
||||
if (asnlen < 15)
|
||||
return;
|
||||
seq3 = ptr;
|
||||
ptr++;
|
||||
|
||||
// is it an OID ?
|
||||
if (*ptr != SNMP_OID)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
|
||||
oidlen = *ptr;
|
||||
if (oidlen != 11)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
// and now parse the OID !!!
|
||||
if (memcmp(ptr, SNMP_UWSGI_BASE, 9))
|
||||
return;
|
||||
|
||||
ptr += 9;
|
||||
|
||||
oid_part[0] = *ptr;
|
||||
|
||||
if (oid_part[0] != 1 && oid_part[0] != 2)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
oid_part[1] = *ptr;
|
||||
if (oid_part[1] < 1 || oid_part[1] > 100)
|
||||
return;
|
||||
ptr++;
|
||||
|
||||
// check for null
|
||||
if (memcmp((char *) ptr, "\x05\x00", 2))
|
||||
return;
|
||||
ptr += 2;
|
||||
|
||||
|
||||
size = build_snmp_response(oid_part[0], oid_part[1], buffer, size, seq1, seq2, seq3);
|
||||
|
||||
if (size > 0) {
|
||||
if (sendto(fd, buffer, size, 0, (struct sockaddr *) client_addr, sizeof(struct sockaddr_in)) < 0) {
|
||||
uwsgi_error("sendto()");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static uint64_t get_uwsgi_snmp_value(uint64_t val, uint8_t * oid_t) {
|
||||
|
||||
val--;
|
||||
|
||||
if (uwsgi.shared->snmp_gvalue[val].type) {
|
||||
*oid_t = uwsgi.shared->snmp_gvalue[val].type;
|
||||
return *uwsgi.shared->snmp_gvalue[val].val;
|
||||
}
|
||||
|
||||
*oid_t = SNMP_NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t get_uwsgi_custom_snmp_value(uint64_t val, uint8_t * oid_t) {
|
||||
|
||||
val--;
|
||||
if (uwsgi.shared->snmp_value[val].type) {
|
||||
*oid_t = uwsgi.shared->snmp_value[val].type;
|
||||
return uwsgi.shared->snmp_value[val].val;
|
||||
}
|
||||
|
||||
*oid_t = SNMP_NULL;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int get_snmp_integer(uint8_t * ptr, uint64_t * val) {
|
||||
|
||||
uint16_t tlen;
|
||||
int i, j;
|
||||
|
||||
tlen = *ptr;
|
||||
|
||||
if (tlen > 4)
|
||||
return -1;
|
||||
|
||||
|
||||
j = 0;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
for (i = 0; i < tlen; i++) {
|
||||
#else
|
||||
for (i = tlen - 1; i >= 0; i--) {
|
||||
#endif
|
||||
val[j] = ptr[1 + i];
|
||||
j++;
|
||||
}
|
||||
|
||||
return tlen + 1;
|
||||
}
|
||||
|
||||
static uint8_t snmp_int_to_snmp(uint64_t snmp_val, uint8_t oid_type, uint8_t * buffer) {
|
||||
|
||||
uint8_t tlen;
|
||||
int i, j;
|
||||
uint8_t *ptr = (uint8_t *) &snmp_val;
|
||||
|
||||
|
||||
// check for counter, counter64 or gauge
|
||||
|
||||
if (oid_type == SNMP_COUNTER64) {
|
||||
tlen = 8;
|
||||
}
|
||||
else if (oid_type == SNMP_NULL || oid_type == 0) {
|
||||
tlen = 0;
|
||||
}
|
||||
else {
|
||||
tlen = 4;
|
||||
}
|
||||
|
||||
buffer[0] = tlen;
|
||||
|
||||
j = 1;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
for (i = 0; i < tlen; i++) {
|
||||
#else
|
||||
for (i = tlen - 1; i >= 0; i--) {
|
||||
#endif
|
||||
buffer[j] = ptr[i];
|
||||
j++;
|
||||
}
|
||||
|
||||
return tlen + 1;
|
||||
}
|
||||
|
||||
static ssize_t build_snmp_response(uint8_t oid1, uint8_t oid2, uint8_t * buffer, int size, uint8_t * seq1, uint8_t * seq2, uint8_t * seq3) {
|
||||
|
||||
uint64_t snmp_val;
|
||||
uint8_t oid_sz;
|
||||
uint8_t oid_type;
|
||||
|
||||
if (oid1 == 1) {
|
||||
snmp_val = get_uwsgi_snmp_value(oid2, &oid_type);
|
||||
}
|
||||
else if (oid1 == 2) {
|
||||
snmp_val = get_uwsgi_custom_snmp_value(oid2, &oid_type);
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
buffer[size - 2] = oid_type;
|
||||
oid_sz = snmp_int_to_snmp(snmp_val, oid_type, buffer + (size - 1));
|
||||
|
||||
if (oid_sz < 1)
|
||||
return -1;
|
||||
|
||||
oid_sz--;
|
||||
|
||||
buffer[1] += oid_sz;
|
||||
*seq1 += oid_sz;
|
||||
*seq2 += oid_sz;
|
||||
*seq3 += oid_sz;
|
||||
|
||||
return size + oid_sz;
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_snmp_counter32(PyObject * self, PyObject * args) {
|
||||
|
||||
uint8_t oid_num;
|
||||
uint32_t oid_val = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "bI:snmp_set_counter32", &oid_num, &oid_val)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (oid_num > 100 || oid_num < 1)
|
||||
goto clear;
|
||||
|
||||
uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER32;
|
||||
uwsgi.shared->snmp_value[oid_num - 1].val = oid_val;
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
clear:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_snmp_counter64(PyObject * self, PyObject * args) {
|
||||
|
||||
uint8_t oid_num;
|
||||
uint64_t oid_val = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "bK:snmp_set_counter64", &oid_num, &oid_val)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (oid_num > 100 || oid_num < 1)
|
||||
goto clear;
|
||||
|
||||
uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_COUNTER64;
|
||||
uwsgi.shared->snmp_value[oid_num - 1].val = oid_val;
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
clear:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_snmp_gauge(PyObject * self, PyObject * args) {
|
||||
|
||||
uint8_t oid_num;
|
||||
uint32_t oid_val = 0;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "bI:snmp_set_gauge", &oid_num, &oid_val)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (oid_num > 100 || oid_num < 1)
|
||||
goto clear;
|
||||
|
||||
uwsgi.shared->snmp_value[oid_num - 1].type = SNMP_GAUGE;
|
||||
uwsgi.shared->snmp_value[oid_num - 1].val = oid_val;
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
clear:
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_snmp_community(PyObject * self, PyObject * args) {
|
||||
|
||||
char *snmp_community;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s:snmp_set_community", &snmp_community)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (strlen(snmp_community) > 72) {
|
||||
uwsgi_log( "*** warning the supplied SNMP community string will be truncated to 72 chars ***\n");
|
||||
memcpy(uwsgi.shared->snmp_community, snmp_community, 72);
|
||||
}
|
||||
else {
|
||||
memcpy(uwsgi.shared->snmp_community, snmp_community, strlen(snmp_community) + 1);
|
||||
}
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
}
|
||||
|
||||
static PyMethodDef uwsgi_snmp_methods[] = {
|
||||
{"snmp_set_counter32", py_snmp_counter32, METH_VARARGS, ""},
|
||||
{"snmp_set_counter64", py_snmp_counter64, METH_VARARGS, ""},
|
||||
{"snmp_set_gauge", py_snmp_gauge, METH_VARARGS, ""},
|
||||
{"snmp_set_community", py_snmp_community, METH_VARARGS, ""},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
void snmp_init() {
|
||||
|
||||
PyMethodDef *uwsgi_function;
|
||||
|
||||
for (uwsgi_function = uwsgi_snmp_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) {
|
||||
PyObject *func = PyCFunction_New(uwsgi_function, NULL);
|
||||
PyDict_SetItemString(uwsgi.embedded_dict, uwsgi_function->ml_name, func);
|
||||
Py_DECREF(func);
|
||||
}
|
||||
|
||||
uwsgi_log( "SNMP python functions initialized.\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#warning "*** SNMP support is disabled ***"
|
||||
#endif
|
||||
@@ -1,75 +1,293 @@
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abstract_socket) {
|
||||
|
||||
int serverfd;
|
||||
struct sockaddr_un *uws_addr;
|
||||
|
||||
fprintf(stderr, "binding on UNIX socket: %s\n", socket_name);
|
||||
uwsgi_log( "binding on UNIX socket: %s\n", socket_name);
|
||||
|
||||
// leave 1 byte for abstract namespace (108 linux -> 104 bsd/mac)
|
||||
if (strlen(socket_name) > 102) {
|
||||
fprintf(stderr, "invalid socket name\n");
|
||||
exit(1);
|
||||
}
|
||||
// leave 1 byte for abstract namespace (108 linux -> 104 bsd/mac)
|
||||
if (strlen(socket_name) > 102) {
|
||||
uwsgi_log( "invalid socket name\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uws_addr = malloc(sizeof(struct sockaddr_un));
|
||||
if (uws_addr == NULL) {
|
||||
perror("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
uws_addr = malloc(sizeof(struct sockaddr_un));
|
||||
if (uws_addr == NULL) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(uws_addr, 0, sizeof(struct sockaddr_un)) ;
|
||||
serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
exit(1);
|
||||
}
|
||||
if (abstract_socket == 0) {
|
||||
if (unlink(socket_name) != 0) {
|
||||
perror("unlink()");
|
||||
}
|
||||
}
|
||||
memset(uws_addr, 0, sizeof(struct sockaddr_un));
|
||||
serverfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
if (abstract_socket == 0) {
|
||||
if (unlink(socket_name) != 0 && errno != ENOENT) {
|
||||
uwsgi_error("unlink()");
|
||||
}
|
||||
}
|
||||
|
||||
if (abstract_socket ==1) {
|
||||
fprintf(stderr, "setting abstract socket mode (warning: only Linux supports this)\n");
|
||||
}
|
||||
if (abstract_socket == 1) {
|
||||
uwsgi_log( "setting abstract socket mode (warning: only Linux supports this)\n");
|
||||
}
|
||||
|
||||
uws_addr->sun_family = AF_UNIX;
|
||||
strcpy(uws_addr->sun_path+abstract_socket, socket_name);
|
||||
memcpy(uws_addr->sun_path + abstract_socket, socket_name, 102);
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) uws_addr, strlen(socket_name)+ abstract_socket + ( (void *)uws_addr->sun_path - (void *)uws_addr) ) != 0) {
|
||||
perror("bind()");
|
||||
if (bind(serverfd, (struct sockaddr *) uws_addr, strlen(socket_name) + abstract_socket + ((void *) uws_addr->sun_path - (void *) uws_addr)) != 0) {
|
||||
uwsgi_error("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
perror("listen()");
|
||||
exit(1);
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// chmod unix socket for lazy users
|
||||
if (chmod_socket == 1 && abstract_socket == 0) {
|
||||
fprintf(stderr, "chmod() socket to 666 for lazy and brave users\n");
|
||||
if (chmod(socket_name, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) != 0) {
|
||||
perror("chmod()");
|
||||
}
|
||||
if (uwsgi.chmod_socket_value) {
|
||||
if (chmod(socket_name, uwsgi.chmod_socket_value) != 0) {
|
||||
uwsgi_error("chmod()");
|
||||
}
|
||||
}
|
||||
else {
|
||||
uwsgi_log( "chmod() socket to 666 for lazy and brave users\n");
|
||||
if (chmod(socket_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != 0) {
|
||||
uwsgi_error("chmod()");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(uws_addr);
|
||||
|
||||
return serverfd;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_SCTP
|
||||
|
||||
#define MAX_SCTP_ADDRESS 4
|
||||
/* sctp address format sctp:127.0.0.1,192.168.0.17:3031 */
|
||||
int bind_to_sctp(char *socket_name, int listen_queue, char *sctp_port) {
|
||||
int serverfd;
|
||||
struct sockaddr_in uws_addr[MAX_SCTP_ADDRESS];
|
||||
int num_ip = 0;
|
||||
|
||||
struct sctp_initmsg sctp_im;
|
||||
|
||||
|
||||
sctp_port[0] = 0;
|
||||
memset(uws_addr, 0, sizeof(struct sockaddr_in) * MAX_SCTP_ADDRESS);
|
||||
memset(&sctp_im, 0, sizeof(struct sctp_initmsg));
|
||||
|
||||
while (socket_name != NULL && num_ip < MAX_SCTP_ADDRESS) {
|
||||
char *ap;
|
||||
while ((ap = strsep(&socket_name, ",")) != NULL) {
|
||||
if (*ap != '\0') {
|
||||
uws_addr[num_ip].sin_family = AF_INET;
|
||||
uws_addr[num_ip].sin_port = htons(atoi(sctp_port + 1));
|
||||
uws_addr[num_ip].sin_addr.s_addr = inet_addr(ap);
|
||||
num_ip++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
|
||||
if (serverfd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi_log( "binding on %d SCTP interfaces on port: %d\n", num_ip, ntohs(uws_addr[0].sin_port));
|
||||
|
||||
|
||||
if (sctp_bindx(serverfd, (struct sockaddr *) uws_addr, num_ip, SCTP_BINDX_ADD_ADDR) != 0) {
|
||||
uwsgi_error("sctp_bindx()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sctp_im.sinit_max_instreams = 0xFFFF;
|
||||
sctp_im.sinit_num_ostreams = 0xFFFF;
|
||||
|
||||
if (setsockopt(serverfd, IPPROTO_SCTP, SCTP_INITMSG, &sctp_im, sizeof(sctp_im))) {
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return serverfd;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef UWSGI_UDP
|
||||
int bind_to_udp(char *socket_name) {
|
||||
int serverfd;
|
||||
struct sockaddr_in uws_addr;
|
||||
char *udp_port;
|
||||
|
||||
#ifdef UWSGI_MULTICAST
|
||||
struct ip_mreq mc;
|
||||
#endif
|
||||
|
||||
udp_port = strchr(socket_name, ':');
|
||||
if (udp_port == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
udp_port[0] = 0;
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(atoi(udp_port + 1));
|
||||
|
||||
if (socket_name[0] != 0) {
|
||||
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
|
||||
}
|
||||
else {
|
||||
uws_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
|
||||
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (serverfd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_MULTICAST
|
||||
if (uwsgi.multicast_group) {
|
||||
uws_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
// if multicast is enabled remember to bind to INADDR_ANY
|
||||
mc.imr_multiaddr.s_addr = inet_addr(uwsgi.multicast_group);
|
||||
if (socket_name[0] == 0) {
|
||||
mc.imr_interface.s_addr = INADDR_ANY;
|
||||
}
|
||||
else {
|
||||
mc.imr_interface.s_addr = inet_addr(socket_name);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
uwsgi_log( "binding on UDP port: %d\n", ntohs(uws_addr.sin_port));
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr)) != 0) {
|
||||
uwsgi_error("bind()");
|
||||
close(serverfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef UWSGI_MULTICAST
|
||||
if (uwsgi.multicast_group) {
|
||||
uwsgi_log( "joining uWSGI multicast group: %s:%d\n", uwsgi.multicast_group, ntohs(uws_addr.sin_port));
|
||||
if (setsockopt(serverfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mc, sizeof(mc))) {
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return serverfd;
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
int uwsgi_connect(char *socket_name, int timeout) {
|
||||
|
||||
char *tcp_port = strchr(socket_name, ':');
|
||||
|
||||
if (tcp_port) {
|
||||
tcp_port[0] = 0;
|
||||
tcp_port++;
|
||||
return connect_to_tcp(socket_name, atoi(tcp_port), timeout);
|
||||
}
|
||||
|
||||
return connect_to_unix(socket_name, timeout);
|
||||
}
|
||||
|
||||
int connect_to_unix(char *socket_name, int timeout) {
|
||||
|
||||
struct pollfd uwsgi_poll;
|
||||
struct sockaddr_un uws_addr;
|
||||
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
uws_addr.sun_family = AF_UNIX;
|
||||
memcpy(uws_addr.sun_path, socket_name, 102);
|
||||
|
||||
uwsgi_poll.fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_un), timeout)) {
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return uwsgi_poll.fd;
|
||||
|
||||
}
|
||||
|
||||
int connect_to_tcp(char *socket_name, int port, int timeout) {
|
||||
|
||||
struct pollfd uwsgi_poll;
|
||||
struct sockaddr_in uws_addr;
|
||||
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(port);
|
||||
|
||||
if (socket_name[0] == 0) {
|
||||
uws_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
else {
|
||||
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
|
||||
}
|
||||
|
||||
socket_name[strlen(socket_name)] = ':' ;
|
||||
|
||||
uwsgi_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (uwsgi_poll.fd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_poll.events = POLLIN;
|
||||
|
||||
if (timed_connect(&uwsgi_poll, (const struct sockaddr *) &uws_addr, sizeof(struct sockaddr_in), timeout)) {
|
||||
uwsgi_error("connect()");
|
||||
close(uwsgi_poll.fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return uwsgi_poll.fd;
|
||||
|
||||
}
|
||||
|
||||
int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
|
||||
int serverfd;
|
||||
struct sockaddr_in uws_addr;
|
||||
int reuse = 1 ;
|
||||
|
||||
tcp_port[0] = 0 ;
|
||||
int reuse = 1;
|
||||
|
||||
tcp_port[0] = 0;
|
||||
memset(&uws_addr, 0, sizeof(struct sockaddr_in));
|
||||
|
||||
uws_addr.sin_family = AF_INET;
|
||||
uws_addr.sin_port = htons(atoi(tcp_port+1));
|
||||
uws_addr.sin_port = htons(atoi(tcp_port + 1));
|
||||
|
||||
|
||||
if (socket_name[0] == 0) {
|
||||
@@ -79,29 +297,117 @@ int bind_to_tcp(char *socket_name, int listen_queue, char *tcp_port) {
|
||||
uws_addr.sin_addr.s_addr = inet_addr(socket_name);
|
||||
}
|
||||
|
||||
serverfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
perror("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&reuse , sizeof(int)) < 0) {
|
||||
perror("setsockopt()");
|
||||
serverfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (serverfd < 0) {
|
||||
uwsgi_error("socket()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse, sizeof(int)) < 0) {
|
||||
uwsgi_error("setsockopt()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fprintf(stderr,"binding on TCP port: %d\n", ntohs(uws_addr.sin_port));
|
||||
if (!uwsgi.no_defer_accept) {
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr) ) != 0) {
|
||||
perror("bind()");
|
||||
#ifdef __linux__
|
||||
if (setsockopt(serverfd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], sizeof(int))) {
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
// OSX has no SO_ACCEPTFILTER !!!
|
||||
#elif defined(__freebsd__)
|
||||
struct accept_filter_arg afa;
|
||||
strcpy(afa.af_name, "dataready");
|
||||
afa.af_arg[0] = 0;
|
||||
if (setsockopt(serverfd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(struct accept_filter_arg))) {
|
||||
uwsgi_error("setsockopt()");
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
uwsgi_log( "binding on TCP port: %d\n", ntohs(uws_addr.sin_port));
|
||||
|
||||
if (bind(serverfd, (struct sockaddr *) &uws_addr, sizeof(uws_addr)) != 0) {
|
||||
if (errno == EADDRINUSE) {
|
||||
uwsgi_log("probably another instance of uWSGI is running on the same address.\n");
|
||||
}
|
||||
uwsgi_error("bind()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (listen(serverfd, listen_queue) != 0) {
|
||||
perror("listen()");
|
||||
exit(1);
|
||||
uwsgi_error("listen()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
return serverfd;
|
||||
}
|
||||
|
||||
int timed_connect(struct pollfd *fdpoll, const struct sockaddr *addr, int addr_size, int timeout) {
|
||||
|
||||
int arg, ret;
|
||||
int soopt;
|
||||
socklen_t solen = sizeof(int);
|
||||
int cnt;
|
||||
/* set non-blocking socket */
|
||||
|
||||
arg = fcntl(fdpoll->fd, F_GETFL, NULL);
|
||||
if (arg < 0) {
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
arg |= O_NONBLOCK;
|
||||
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = connect(fdpoll->fd, addr, addr_size);
|
||||
if (ret < 0) {
|
||||
/* check what happened */
|
||||
|
||||
// in progress ?
|
||||
if (errno == EINPROGRESS) {
|
||||
if (timeout < 1)
|
||||
timeout = 3;
|
||||
fdpoll->events = POLLOUT;
|
||||
cnt = poll(fdpoll, 1, timeout * 1000);
|
||||
/* check for errors */
|
||||
if (cnt < 0 && errno != EINTR) {
|
||||
uwsgi_error("poll()");
|
||||
return -1;
|
||||
}
|
||||
/* something hapened on the socket ... */
|
||||
else if (cnt > 0) {
|
||||
if (getsockopt(fdpoll->fd, SOL_SOCKET, SO_ERROR, (void *) (&soopt), &solen) < 0) {
|
||||
uwsgi_error("getsockopt()");
|
||||
return -1;
|
||||
}
|
||||
/* is something bad ? */
|
||||
if (soopt) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* timeout */
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* re-set blocking socket */
|
||||
arg &= (~O_NONBLOCK);
|
||||
if (fcntl(fdpoll->fd, F_SETFL, arg) < 0) {
|
||||
uwsgi_error("fcntl()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,116 +1,105 @@
|
||||
#ifdef UWSGI_SPOOLER
|
||||
#include "uwsgi.h"
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
int spool_request(struct uwsgi_server *uwsgi, char *filename, int rn, char *buffer, int size) {
|
||||
|
||||
extern char *spool_dir;
|
||||
|
||||
struct uwsgi_packet_header {
|
||||
uint8_t modifier1;
|
||||
uint16_t datasize;
|
||||
uint8_t modifier2;
|
||||
};
|
||||
|
||||
|
||||
int spool_request(char *filename, int rn, char *buffer, int size) {
|
||||
|
||||
char hostname[256+1];
|
||||
struct timeval tv;
|
||||
char hostname[256 + 1];
|
||||
struct timeval tv;
|
||||
int fd;
|
||||
struct uwsgi_packet_header uh ;
|
||||
struct uwsgi_header uh;
|
||||
|
||||
if (gethostname(hostname,256)) {
|
||||
perror("gethostname()");
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
hostname[256] = 0 ;
|
||||
|
||||
if (snprintf(filename,1024,"%s/uwsgi_spoolfile_on_%s_%d_%d_%llu_%llu", spool_dir, hostname, getpid(), rn, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec ) <= 0) {
|
||||
if (gethostname(hostname, 256)) {
|
||||
uwsgi_error("gethostname()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
hostname[256] = 0;
|
||||
|
||||
if (snprintf(filename, 1024, "%s/uwsgi_spoolfile_on_%s_%d_%d_%llu_%llu", uwsgi->spool_dir, hostname, (int) getpid(), rn, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec) <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
|
||||
if (fd < 0) {
|
||||
perror("open()");
|
||||
return 0 ;
|
||||
uwsgi_error("open()");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __sun__
|
||||
if (lockf(fd, F_LOCK, 0)) {
|
||||
perror("lockf()");
|
||||
uwsgi_error("lockf()");
|
||||
#else
|
||||
if (flock(fd, LOCK_EX)) {
|
||||
perror("flock()");
|
||||
uwsgi_error("flock()");
|
||||
#endif
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uh.modifier1 = 17 ;
|
||||
uh.modifier2 = 0 ;
|
||||
uh.datasize = (uint16_t) size ;
|
||||
uh.modifier1 = 17;
|
||||
uh.modifier2 = 0;
|
||||
uh.pktsize = (uint16_t) size;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh.datasize= uwsgi_swap16(uh.datasize);
|
||||
uh.pktsize = uwsgi_swap16(uh.pktsize);
|
||||
#endif
|
||||
|
||||
if (write(fd, &uh, 4) != 4) {
|
||||
goto clear ;
|
||||
goto clear;
|
||||
}
|
||||
|
||||
if (write(fd, buffer, size) != size) {
|
||||
goto clear ;
|
||||
goto clear;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
fprintf(stderr,"written %d bytes to spool file %s.\n",size + 4, filename);
|
||||
uwsgi_log( "written %d bytes to spool file %s.\n", size + 4, filename);
|
||||
|
||||
return 1;
|
||||
|
||||
|
||||
clear:
|
||||
|
||||
perror("write()");
|
||||
clear:
|
||||
|
||||
uwsgi_error("write()");
|
||||
unlink(filename);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void spooler(PyObject *uwsgi_module) {
|
||||
DIR *sdir ;
|
||||
void spooler(struct uwsgi_server *uwsgi, PyObject * uwsgi_module_dict) {
|
||||
DIR *sdir;
|
||||
struct dirent *dp;
|
||||
PyObject *uwsgi_module_dict, *spooler_callable, *spool_result, *spool_tuple, *spool_env ;
|
||||
int spool_fd ;
|
||||
uint16_t uwstrlen ;
|
||||
PyObject *spooler_callable, *spool_result, *spool_tuple, *spool_env;
|
||||
int spool_fd;
|
||||
uint16_t uwstrlen;
|
||||
int rlen = 0;
|
||||
int datasize ;
|
||||
int datasize;
|
||||
|
||||
struct uwsgi_packet_header uh ;
|
||||
// prevent process blindly reading stdin to make mess
|
||||
int nullfd;
|
||||
|
||||
struct uwsgi_header uh;
|
||||
|
||||
char *key;
|
||||
char *val;
|
||||
|
||||
uwsgi_module_dict = PyModule_GetDict(uwsgi_module);
|
||||
if (!uwsgi_module_dict) {
|
||||
fprintf(stderr,"could not get uwsgi module __dict__\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// spool every 30 seconds by default
|
||||
uwsgi->shared->spooler_frequency = 30 ;
|
||||
|
||||
spool_tuple = PyTuple_New(1);
|
||||
|
||||
if (!spool_tuple) {
|
||||
fprintf(stderr,"could not create spooler tuple.\n");
|
||||
uwsgi_log( "could not create spooler tuple.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
spool_env = PyDict_New();
|
||||
if (!spool_env) {
|
||||
fprintf(stderr,"could not create spooler env.\n");
|
||||
uwsgi_log( "could not create spooler env.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -119,18 +108,33 @@ void spooler(PyObject *uwsgi_module) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (chdir(spool_dir)) {
|
||||
perror("chdir()");
|
||||
if (chdir(uwsgi->spool_dir)) {
|
||||
uwsgi_error("chdir()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
sdir = opendir(".");
|
||||
// asked by Marco Beri
|
||||
uwsgi_log( "lowering spooler priority to %d\n", PRIO_MAX);
|
||||
setpriority(PRIO_PROCESS, getpid(), PRIO_MAX);
|
||||
|
||||
nullfd = open("/dev/null", O_RDONLY);
|
||||
if (nullfd < 0) {
|
||||
uwsgi_error("open()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (nullfd != 0) {
|
||||
dup2(nullfd, 0);
|
||||
close(nullfd);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
|
||||
sleep(uwsgi->shared->spooler_frequency);
|
||||
|
||||
sdir = opendir(uwsgi->spool_dir);
|
||||
if (sdir) {
|
||||
while((dp = readdir(sdir)) != NULL) {
|
||||
#ifndef __sun__
|
||||
if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19) && dp->d_type == DT_REG) {
|
||||
#else
|
||||
while ((dp = readdir(sdir)) != NULL) {
|
||||
if (!strncmp("uwsgi_spoolfile_on_", dp->d_name, 19)) {
|
||||
struct stat sf_lstat;
|
||||
if (lstat(dp->d_name, &sf_lstat)) {
|
||||
@@ -139,94 +143,93 @@ void spooler(PyObject *uwsgi_module) {
|
||||
if (!S_ISREG(sf_lstat.st_mode)) {
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (!access(dp->d_name, R_OK|W_OK)) {
|
||||
fprintf(stderr,"managing spool request %s...\n", dp->d_name);
|
||||
if (!access(dp->d_name, R_OK | W_OK)) {
|
||||
uwsgi_log( "managing spool request %s...\n", dp->d_name);
|
||||
|
||||
spooler_callable = PyDict_GetItemString(uwsgi_module_dict, "spooler");
|
||||
if (!spooler_callable) {
|
||||
fprintf(stderr,"you have to define uwsgi.spooler to use the spooler !!!\n");
|
||||
uwsgi_log( "you have to define uwsgi.spooler to use the spooler !!!\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
spool_fd = open(dp->d_name, O_RDONLY) ;
|
||||
spool_fd = open(dp->d_name, O_RDONLY);
|
||||
if (spool_fd < 0) {
|
||||
perror("open()");
|
||||
uwsgi_error("open()");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __sun__
|
||||
if (lockf(spool_fd, F_LOCK, 0)) {
|
||||
perror("lockf()");
|
||||
uwsgi_error("lockf()");
|
||||
#else
|
||||
if (flock(spool_fd, LOCK_EX)) {
|
||||
perror("flock()");
|
||||
uwsgi_error("flock()");
|
||||
#endif
|
||||
close(spool_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (read(spool_fd, &uh, 4) != 4) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
close(spool_fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh.datasize= uwsgi_swap16(uh.datasize);
|
||||
#endif
|
||||
|
||||
datasize = 0 ;
|
||||
#ifdef __BIG_ENDIAN__
|
||||
uh.pktsize = uwsgi_swap16(uh.pktsize);
|
||||
#endif
|
||||
|
||||
while( datasize < uh.datasize) {
|
||||
rlen = read(spool_fd, &uwstrlen, 2) ;
|
||||
datasize = 0;
|
||||
|
||||
while (datasize < uh.pktsize) {
|
||||
rlen = read(spool_fd, &uwstrlen, 2);
|
||||
if (rlen != 2) {
|
||||
perror("read()");
|
||||
goto next_spool ;
|
||||
uwsgi_error("read()");
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen ;
|
||||
datasize += rlen;
|
||||
key = NULL;
|
||||
val = NULL;
|
||||
if (uwstrlen > 0) {
|
||||
key = malloc(uwstrlen+1);
|
||||
key = malloc(uwstrlen + 1);
|
||||
if (!key) {
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
goto retry_later;
|
||||
}
|
||||
rlen = read(spool_fd, key, uwstrlen);
|
||||
if (rlen != uwstrlen) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen ;
|
||||
key[rlen] = 0 ;
|
||||
datasize += rlen;
|
||||
key[rlen] = 0;
|
||||
|
||||
|
||||
rlen = read(spool_fd, &uwstrlen, 2);
|
||||
if (rlen != 2) {
|
||||
perror("read()");
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen ;
|
||||
datasize += rlen;
|
||||
|
||||
if (uwstrlen > 0) {
|
||||
val = malloc(uwstrlen+1);
|
||||
val = malloc(uwstrlen + 1);
|
||||
if (!val) {
|
||||
free(key);
|
||||
perror("malloc()");
|
||||
uwsgi_error("malloc()");
|
||||
goto retry_later;
|
||||
}
|
||||
|
||||
rlen = read(spool_fd, val, uwstrlen);
|
||||
if (rlen != uwstrlen) {
|
||||
perror("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen ;
|
||||
val[rlen] = 0 ;
|
||||
uwsgi_error("read()");
|
||||
free(key);
|
||||
goto next_spool;
|
||||
}
|
||||
datasize += rlen;
|
||||
val[rlen] = 0;
|
||||
/* ready to add item to the dict */
|
||||
}
|
||||
|
||||
@@ -234,7 +237,7 @@ void spooler(PyObject *uwsgi_module) {
|
||||
PyErr_Print();
|
||||
free(key);
|
||||
free(val);
|
||||
goto retry_later ;
|
||||
goto retry_later;
|
||||
}
|
||||
|
||||
free(key);
|
||||
@@ -244,33 +247,33 @@ void spooler(PyObject *uwsgi_module) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
spool_result = PyEval_CallObject(spooler_callable, spool_tuple);
|
||||
|
||||
spool_result = python_call(spooler_callable, spool_tuple, 0);
|
||||
if (!spool_result) {
|
||||
PyErr_Print();
|
||||
fprintf(stderr,"error detected. spool request canceled.\n");
|
||||
uwsgi_log( "error detected. spool request canceled.\n");
|
||||
goto next_spool;
|
||||
}
|
||||
if (PyInt_Check(spool_result)) {
|
||||
if (PyInt_AsLong(spool_result) == 17) {
|
||||
Py_DECREF(spool_result);
|
||||
fprintf(stderr,"retry this task later...\n");
|
||||
uwsgi_log( "retry this task later...\n");
|
||||
goto retry_later;
|
||||
}
|
||||
}
|
||||
|
||||
Py_DECREF(spool_result);
|
||||
|
||||
fprintf(stderr,"done with task/spool %s\n", dp->d_name);
|
||||
next_spool:
|
||||
uwsgi_log( "done with task/spool %s\n", dp->d_name);
|
||||
next_spool:
|
||||
|
||||
if (unlink(dp->d_name)) {
|
||||
perror("unlink");
|
||||
fprintf(stderr,"something horrible happened to the spooler. Better to kill it.\n");
|
||||
uwsgi_error("unlink");
|
||||
uwsgi_log( "something horrible happened to the spooler. Better to kill it.\n");
|
||||
exit(1);
|
||||
}
|
||||
retry_later:
|
||||
retry_later:
|
||||
PyDict_Clear(spool_env);
|
||||
close(spool_fd);
|
||||
}
|
||||
@@ -279,10 +282,58 @@ retry_later:
|
||||
closedir(sdir);
|
||||
}
|
||||
else {
|
||||
perror("opendir()");
|
||||
uwsgi_error("opendir()");
|
||||
}
|
||||
|
||||
/* TODO spooler frequency user-configurable */
|
||||
sleep(5);
|
||||
}
|
||||
}
|
||||
|
||||
int uwsgi_request_spooler(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int i;
|
||||
char spool_filename[1024];
|
||||
|
||||
if (uwsgi->spool_dir == NULL) {
|
||||
uwsgi_log( "the spooler is inactive !!!...skip\n");
|
||||
wsgi_req->uh.modifier1 = 255;
|
||||
wsgi_req->uh.pktsize = 0;
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
i = write(wsgi_req->poll.fd, wsgi_req, 4);
|
||||
if (i != 4) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
uwsgi_log( "managing spool request...\n");
|
||||
i = spool_request(uwsgi, spool_filename, uwsgi->workers[0].requests + 1, wsgi_req->buffer, wsgi_req->uh.pktsize);
|
||||
wsgi_req->uh.modifier1 = 255;
|
||||
wsgi_req->uh.pktsize = 0;
|
||||
if (i > 0) {
|
||||
wsgi_req->uh.modifier2 = 1;
|
||||
if (write(wsgi_req->poll.fd, wsgi_req, 4) != 4) {
|
||||
uwsgi_log( "disconnected client, remove spool file.\n");
|
||||
/* client disconnect, remove spool file */
|
||||
if (unlink(spool_filename)) {
|
||||
uwsgi_error("unlink()");
|
||||
uwsgi_log( "something horrible happened !!! check your spooler ASAP !!!\n");
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
/* announce a failed spool request */
|
||||
wsgi_req->uh.modifier2 = 0;
|
||||
i = write(wsgi_req->poll.fd, wsgi_req, 4);
|
||||
if (i != 4) {
|
||||
uwsgi_error("write()");
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else
|
||||
#warning "*** Spooler support is disabled ***"
|
||||
#endif
|
||||
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
#ifdef UWSGI_STACKLESS
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
struct wsgi_request* find_request_by_tasklet(PyTaskletObject *tasklet) {
|
||||
|
||||
int i ;
|
||||
for(i=0;i<uwsgi.async;i++) {
|
||||
if (uwsgi.stackless_table[i]->tasklet == tasklet) {
|
||||
return uwsgi.stackless_table[i]->wsgi_req;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
PyObject *py_uwsgi_stackless_worker(PyObject * self, PyObject * args) {
|
||||
|
||||
PyThreadState *ts = PyThreadState_GET();
|
||||
struct wsgi_request *wsgi_req = find_request_by_tasklet(ts->st.current);
|
||||
PyObject *zero;
|
||||
|
||||
int async_id = wsgi_req->async_id;
|
||||
|
||||
//uwsgi_log("i am the tasklet worker\n");
|
||||
|
||||
for(;;) {
|
||||
|
||||
// wait for request
|
||||
zero = PyChannel_Receive(uwsgi.workers_channel);
|
||||
|
||||
wsgi_req_setup(wsgi_req, async_id);
|
||||
|
||||
if (wsgi_req_accept(uwsgi.serverfd, wsgi_req)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (wsgi_req_recv(wsgi_req)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uwsgi_close_request(&uwsgi, wsgi_req);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
PyMethodDef uwsgi_stackless_worker[] = { {"uwsgi_stackless_worker", py_uwsgi_stackless_worker, METH_VARARGS, ""} };
|
||||
|
||||
void stackless_init(struct uwsgi_server *uwsgi) {
|
||||
|
||||
int i;
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
|
||||
PyObject *tasklet_worker = PyCFunction_New(uwsgi_stackless_worker, NULL);
|
||||
|
||||
uwsgi->workers_channel = PyChannel_New(NULL);
|
||||
|
||||
uwsgi->stackless_table = malloc( sizeof(struct stackless_req*) * uwsgi->async);
|
||||
if (!uwsgi->stackless_table) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
uwsgi->stackless_table[i] = malloc(sizeof(struct stackless_req));
|
||||
if (!uwsgi->stackless_table[i]) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
memset(uwsgi->stackless_table[i], 0, sizeof(struct stackless_req));
|
||||
}
|
||||
|
||||
uwsgi_log("initializing %d tasklet...", uwsgi->async);
|
||||
|
||||
// creating uwsgi->async tasklets
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
wsgi_req->tasklet = PyTasklet_New(NULL, tasklet_worker);
|
||||
uwsgi->stackless_table[i]->tasklet = wsgi_req->tasklet;
|
||||
uwsgi->stackless_table[i]->wsgi_req = wsgi_req;
|
||||
// useless for now, it will be used for I/O or other messaging
|
||||
uwsgi->stackless_table[i]->channel = NULL;
|
||||
wsgi_req->async_id = i ;
|
||||
|
||||
PyTasklet_Setup(wsgi_req->tasklet, PyTuple_New(0), NULL);
|
||||
//PyTasklet_Run(wsgi_req->tasklet);
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
uwsgi_log("done\n");
|
||||
}
|
||||
|
||||
void stackless_loop(struct uwsgi_server *uwsgi) {
|
||||
|
||||
int i;
|
||||
PyTaskletObject *int_tasklet;
|
||||
|
||||
// tasklets main loop
|
||||
for(;;) {
|
||||
//uwsgi->async_running = -1 ;
|
||||
//if (PyStackless_GetRunCount() > 0) {
|
||||
uwsgi->async_running = 0 ;
|
||||
//}
|
||||
uwsgi->async_nevents = async_wait(uwsgi->async_queue, uwsgi->async_events, uwsgi->async, uwsgi->async_running, 0);
|
||||
|
||||
if (uwsgi->async_nevents < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(i=0; i<uwsgi->async_nevents;i++) {
|
||||
|
||||
if (uwsgi->async_events[i].ASYNC_FD == uwsgi->serverfd) {
|
||||
//pass the connection to the first available tasklet
|
||||
uwsgi_log("sending new connection...\n");
|
||||
PyChannel_Send(uwsgi->workers_channel, Py_True);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
if (PyStackless_GetRunCount() > 0) {
|
||||
PyStackless_Schedule(Py_None, 0);
|
||||
}
|
||||
*/
|
||||
|
||||
PyStackless_RunWatchdogEx( 10, PY_WATCHDOG_TOTALTIMEOUT);
|
||||
|
||||
//int_tasklet = (PyTaskletObject *) PyStackless_RunWatchdog( 1000 );
|
||||
/*
|
||||
uwsgi_log("done watchdog %p\n", int_tasklet);
|
||||
if (!PyTasklet_IsCurrent(int_tasklet)) {
|
||||
uwsgi_log("re-insert: %d\n", 1);// PyTasklet_Insert(int_tasklet));
|
||||
}
|
||||
uwsgi_log("recycle\n");
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
import uwsgi
|
||||
|
||||
counter = 0
|
||||
uwsgi.snmp_set_counter64(1, counter)
|
||||
|
||||
def application(environ, start_response):
|
||||
global counter
|
||||
|
||||
start_response('200 OK', [('Content-Type', 'image/png')])
|
||||
fd = open('logo_uWSGI.png','r')
|
||||
counter = counter+1
|
||||
uwsgi.snmp_set_counter64(1, counter)
|
||||
return environ['wsgi.file_wrapper'](fd, 4096)
|
||||
@@ -0,0 +1,11 @@
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $app = sub {
|
||||
my $env = shift;
|
||||
return [
|
||||
'200',
|
||||
[ 'Content-Type' => 'text/plain' ],
|
||||
[ "Hello World\r\n", $env->{'REQUEST_URI'} ],
|
||||
];
|
||||
};
|
||||
+38
-17
@@ -10,6 +10,7 @@ os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
|
||||
|
||||
#import django.core.handlers.wsgi
|
||||
|
||||
#uwsgi.load_plugin(0, "plugins/example/example_plugin.so", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa")
|
||||
|
||||
from threading import Thread
|
||||
|
||||
@@ -88,6 +89,10 @@ def djangohomepage():
|
||||
def reload(env, start_response):
|
||||
|
||||
start_response('200 OK', [('Content-Type', 'text/html')])
|
||||
|
||||
#uwsgi.sorry_i_need_to_block()
|
||||
#time.sleep(1)
|
||||
|
||||
#uwsgi.reload()
|
||||
|
||||
# print str(uwsgi.masterpid()) + "\n"
|
||||
@@ -115,31 +120,47 @@ def reload(env, start_response):
|
||||
|
||||
# yield 4/0
|
||||
|
||||
out = '<h1>uWSGI status</h1>' ;
|
||||
out += 'masterpid: <b>' + str(uwsgi.masterpid()) + '</b><br/>'
|
||||
yield '<h1>uWSGI status ('+env['SCRIPT_NAME']+')</h1>' ;
|
||||
yield 'masterpid: <b>' + str(uwsgi.masterpid()) + '</b><br/>'
|
||||
|
||||
out += 'started on: <b>' + time.ctime(uwsgi.started_on) + '</b><br/>'
|
||||
yield 'started on: <b>' + time.ctime(uwsgi.started_on) + '</b><br/>'
|
||||
|
||||
out += 'buffer size: <b>' + str(uwsgi.buffer_size) + '</b><br/>'
|
||||
yield 'buffer size: <b>' + str(uwsgi.buffer_size) + '</b><br/>'
|
||||
|
||||
out += 'total_requests: <b>' + str(uwsgi.total_requests()) + '</b><br/>'
|
||||
yield 'total_requests: <b>' + str(uwsgi.total_requests()) + '</b><br/>'
|
||||
|
||||
out += 'workers: <b>' + str(uwsgi.numproc) + '</b><br/>'
|
||||
yield 'workers: <b>' + str(uwsgi.numproc) + '</b><br/>'
|
||||
|
||||
out += '<table border="1">'
|
||||
out += '<th>worker id</th><th>pid</th><th>requests</th><th>address space</th><th>rss</th>'
|
||||
yield '<h2>dynamic options</h2>'
|
||||
|
||||
yield '<b>logging</b>: ' + str(uwsgi.get_option(0)) + '<br/>'
|
||||
yield '<b>max_requests</b>: ' + str(uwsgi.getoption(1)) + '<br/>'
|
||||
yield '<b>socket_timeout</b>: ' + str(uwsgi.getoption(2)) + '<br/>'
|
||||
yield '<b>memory_debug</b>: ' + str(uwsgi.getoption(3)) + '<br/>'
|
||||
yield '<b>master_interval</b>: ' + str(uwsgi.getoption(4)) + '<br/>'
|
||||
yield '<b>harakiri</b>: ' + str(uwsgi.getoption(5)) + '<br/>'
|
||||
yield '<b>cgi_mode</b>: ' + str(uwsgi.get_option(6)) + '<br/>'
|
||||
yield '<b>threads</b>: ' + str(uwsgi.get_option(7)) + '<br/>'
|
||||
yield '<b>process_reaper</b>: ' + str(uwsgi.get_option(8)) + '<br/>'
|
||||
|
||||
yield '<table border="1">'
|
||||
yield '<th>worker id</th><th>pid</th><th>in request</th><th>requests</th><th>running time</th><th>address space</th><th>rss</th>'
|
||||
|
||||
workers = uwsgi.workers();
|
||||
|
||||
for w in workers:
|
||||
print w
|
||||
if w is not None:
|
||||
out += '<tr><td>'+ str(w['id']) +'</td><td>' + str(w['pid']) + '</td><td>' + str(w['requests']) + '</td><td>' + str(w['vsz']) + '</td><td>' + str(w['rss']) + '</td></tr>'
|
||||
|
||||
out += '</table>'
|
||||
yield '<h2>workers</h2>'
|
||||
|
||||
yield out
|
||||
print "FATTOfattoFATTO"
|
||||
for w in workers:
|
||||
#print w
|
||||
#print w['running_time']
|
||||
if w is not None:
|
||||
yield '<tr><td>'+ str(w['id']) +'</td><td>' + str(w['pid']) + '</td><td>' + str(w['pid']) + '</td><td>' + str(w['requests']) + '</td><td>' + str(w['running_time']) + '</td><td>' + str(w['vsz']) + '</td><td>' + str(w['rss']) + '</td></tr>'
|
||||
print w
|
||||
|
||||
yield '</table>'
|
||||
|
||||
#yield out
|
||||
#print "FATTOfattoFATTO"
|
||||
|
||||
def remotemako(env, start_response):
|
||||
start_response('200 OK', [('Content-Type', 'text/html')])
|
||||
@@ -162,7 +183,7 @@ uwsgi.fastfuncs.insert(17, djangohomepage)
|
||||
#djangoapp = django.core.handlers.wsgi.WSGIHandler()
|
||||
|
||||
#applications = { '/':django.core.handlers.wsgi.WSGIHandler() }
|
||||
uwsgi.applications = { '/':reload }
|
||||
uwsgi.applications = { '/':reload, '/pippo':reload }
|
||||
|
||||
print uwsgi.applications
|
||||
print uwsgi.applist
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import time
|
||||
|
||||
def application(env, start_response):
|
||||
start_response( '200 OK', [ ('Content-Type','text/html') ])
|
||||
for i in range(1,1000):
|
||||
yield "<h1>%s at %s</h1>" % (i, str(time.time()))
|
||||
@@ -0,0 +1,15 @@
|
||||
import uwsgi
|
||||
import time
|
||||
|
||||
def application(env, start_response):
|
||||
counter = 0
|
||||
start_response( '200 OK', [ ('Content-Type','text/html') ])
|
||||
start_time = time.time()
|
||||
for i in range(1,100000):
|
||||
uwsgi.green_schedule()
|
||||
# print every 100
|
||||
if i % 100 == 0:
|
||||
yield "<h1>%d</h1>\n" % i
|
||||
counter = counter + i
|
||||
|
||||
yield "<h1>%d cycles after %d</h1>\n" % (counter, time.time() - start_time)
|
||||
@@ -0,0 +1,14 @@
|
||||
import time
|
||||
import stackless
|
||||
|
||||
def application(env, start_response):
|
||||
start_response( '200 OK', [ ('Content-Type','text/html') ])
|
||||
#print env
|
||||
for i in range(1,100000):
|
||||
#print i
|
||||
yield "<h1>%s at %s</h1>\n" % (i, str(time.time()))
|
||||
#schedule every 2
|
||||
if i % 2 == 0:
|
||||
stackless.schedule()
|
||||
|
||||
print "DONE AT %d" % i
|
||||
@@ -0,0 +1,17 @@
|
||||
import sys
|
||||
import mimetypes
|
||||
|
||||
basedir = sys.argv[1]
|
||||
mimetypes.init()
|
||||
|
||||
|
||||
def application(environ, start_response):
|
||||
|
||||
filename = basedir + environ['PATH_INFO']
|
||||
(content_type, encoding) = mimetypes.guess_type(filename)
|
||||
if not content_type:
|
||||
content_type = 'text/plain'
|
||||
|
||||
start_response('200 OK', [('Content-Type', content_type)])
|
||||
fd = open(filename,'r')
|
||||
yield environ['wsgi.file_wrapper'](fd, 32*1024)
|
||||
@@ -0,0 +1,22 @@
|
||||
import uwsgi
|
||||
import time
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [ ('Content-Type','text/html') ] )
|
||||
|
||||
yield "I am the worker %d<br/>" % uwsgi.worker_id()
|
||||
|
||||
grunt = uwsgi.grunt()
|
||||
|
||||
if grunt is None:
|
||||
print "worker %d detached" % uwsgi.worker_id()
|
||||
else:
|
||||
yield "And now i am the grunt with a fix worker id of %d<br/>" % uwsgi.worker_id()
|
||||
time.sleep(2)
|
||||
yield "Now, i will start a very slow task...<br/>"
|
||||
for i in xrange(1,10):
|
||||
yield "waiting for %d seconds<br/>" % i
|
||||
time.sleep(i)
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import socket
|
||||
import select
|
||||
import errno
|
||||
|
||||
def send_request(env, client):
|
||||
|
||||
client.setblocking(1)
|
||||
|
||||
client.send(b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n")
|
||||
client.send(b"Host: www.google.it\r\n\r\n")
|
||||
|
||||
while 1:
|
||||
yield env['x-wsgiorg.fdevent.readable'](client.fileno(), 10)
|
||||
buf = client.recv(4096)
|
||||
if len(buf) == 0:
|
||||
break
|
||||
else:
|
||||
yield buf
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.setblocking(0)
|
||||
|
||||
#env['x-wsgiorg.fdevent.readable'] = lambda fd,t: ""
|
||||
#env['x-wsgiorg.fdevent.writable'] = lambda fd,t: ""
|
||||
|
||||
#yield ""
|
||||
|
||||
c = s.connect_ex(('www.google.it', 80))
|
||||
if c == errno.EINPROGRESS:
|
||||
yield env['x-wsgiorg.fdevent.writable'](s.fileno(), 10)
|
||||
for r in send_request(env, s):
|
||||
yield r
|
||||
elif c == errno.EISCONN:
|
||||
for r in send_request(env, s):
|
||||
yield r
|
||||
else:
|
||||
start_response( '500 Internal Server Error', [ ('Content-Type', 'text/html')])
|
||||
yield "Internal Server Error"
|
||||
|
||||
s.close()
|
||||
@@ -0,0 +1,41 @@
|
||||
import uwsgi
|
||||
import socket
|
||||
import errno
|
||||
|
||||
def send_request(env, client):
|
||||
|
||||
client.setblocking(1)
|
||||
|
||||
client.send(b"GET /intl/it_it/images/logo.gif HTTP/1.0\r\n")
|
||||
client.send(b"Host: www.google.it\r\n\r\n")
|
||||
|
||||
uwsgi.green_schedule()
|
||||
|
||||
while 1:
|
||||
uwsgi.green_wait_fdread(client.fileno(), 10)
|
||||
buf = client.recv(4096)
|
||||
if len(buf) == 0:
|
||||
break
|
||||
else:
|
||||
yield buf
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.setblocking(0)
|
||||
|
||||
c = s.connect_ex(('74.125.77.104', 80))
|
||||
if c == errno.EINPROGRESS:
|
||||
uwsgi.green_wait_fdwrite(s.fileno(), 10)
|
||||
for r in send_request(env, s):
|
||||
yield r
|
||||
elif c == errno.EISCONN:
|
||||
for r in send_request(env, s):
|
||||
yield r
|
||||
else:
|
||||
uwsgi.green_schedule()
|
||||
start_response( '500 Internal Server Error', [ ('Content-Type', 'text/html')])
|
||||
yield "Internal Server Error"
|
||||
|
||||
s.close()
|
||||
@@ -0,0 +1,19 @@
|
||||
import uwsgi
|
||||
|
||||
from mako.template import Template
|
||||
import time
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
start_response( '200 OK', [ ('Content-Type','text/html') ])
|
||||
|
||||
mytemplate = Template("<h1>I am Mako at ${thetime}</h1>")
|
||||
uwsgi.green_schedule()
|
||||
|
||||
yield mytemplate.render(thetime = time.time() )
|
||||
|
||||
for i in range(1, 100):
|
||||
mytemplate = Template("Iteration ${thei} at ${thetime}<br/>")
|
||||
uwsgi.green_schedule()
|
||||
yield mytemplate.render(thei = i, thetime = time.time() )
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import uwsgi
|
||||
import psycopg2
|
||||
|
||||
|
||||
def ugreen_wait_callback(conn, timeout=-1):
|
||||
"""A wait callback useful to allow uWSGI/uGreen to work with Psycopg."""
|
||||
while True:
|
||||
state = conn.poll()
|
||||
if state == psycopg2.extensions.POLL_OK:
|
||||
break
|
||||
elif state == psycopg2.extensions.POLL_READ:
|
||||
uwsgi.green_wait_fdread(conn.fileno())
|
||||
elif state == psycopg2.extensions.POLL_WRITE:
|
||||
uwsgi.green_wait_fdwrite(conn.fileno())
|
||||
else:
|
||||
raise Exception("Unexpected result from poll: %r", state)
|
||||
|
||||
|
||||
|
||||
# set the wait callback
|
||||
psycopg2.extensions.set_wait_callback(ugreen_wait_callback)
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
|
||||
# connect
|
||||
conn = psycopg2.connect("dbname=prova user=postgres")
|
||||
# get cursor
|
||||
curs = conn.cursor()
|
||||
|
||||
yield "<table>"
|
||||
|
||||
# run query
|
||||
curs.execute("SELECT * FROM tests")
|
||||
|
||||
while True:
|
||||
row = curs.fetchone()
|
||||
if not row: break
|
||||
yield "<tr><td>%s</td></tr>" % str(row)
|
||||
|
||||
yield "</table>"
|
||||
|
||||
conn.close()
|
||||
@@ -0,0 +1,50 @@
|
||||
import uwsgi
|
||||
import psycopg2
|
||||
|
||||
def async_wait(conn):
|
||||
# conn can be a connection or a cursor
|
||||
if not hasattr(conn, 'poll'):
|
||||
conn = conn.connection
|
||||
|
||||
# interesting part: suspend until ready
|
||||
while True:
|
||||
state = conn.poll()
|
||||
if state == psycopg2.extensions.POLL_OK:
|
||||
break
|
||||
elif state == psycopg2.extensions.POLL_READ:
|
||||
uwsgi.green_wait_fdread(conn.fileno())
|
||||
elif state == psycopg2.extensions.POLL_WRITE:
|
||||
uwsgi.green_wait_fdwrite(conn.fileno())
|
||||
else:
|
||||
raise Exception("Unexpected result from poll: %r", state)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
|
||||
conn = psycopg2.connect("dbname=prova user=postgres", async=True)
|
||||
|
||||
# suspend until connection
|
||||
async_wait(conn)
|
||||
|
||||
curs = conn.cursor()
|
||||
|
||||
yield "<table>"
|
||||
|
||||
curs.execute("SELECT * FROM tests")
|
||||
|
||||
# suspend until result
|
||||
async_wait(curs)
|
||||
|
||||
while True:
|
||||
row = curs.fetchone()
|
||||
if not row: break
|
||||
yield "<tr><td>%s</td></tr>" % str(row)
|
||||
|
||||
yield "</table>"
|
||||
|
||||
conn.close()
|
||||
@@ -0,0 +1,9 @@
|
||||
import uwsgi
|
||||
|
||||
sleepvalue = 5 ;
|
||||
|
||||
def application(env, start_response):
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
yield uwsgi.async_sleep(sleepvalue)
|
||||
#print "TIMEOUT: ", env['x-wsgiorg.fdevent.timeout']
|
||||
yield "<h1>Hello World after %d seconds</h1>" % sleepvalue
|
||||
@@ -0,0 +1,14 @@
|
||||
import uwsgi
|
||||
import time
|
||||
|
||||
|
||||
def application(env, start_response):
|
||||
sleepvalue = 5 ;
|
||||
if env.has_key('QUERY_STRING'):
|
||||
if env['QUERY_STRING'] != '':
|
||||
sleepvalue = int(env['QUERY_STRING'])
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
start_at = time.time()
|
||||
uwsgi.green_sleep(sleepvalue)
|
||||
#print "TIMEOUT: ", env['x-wsgiorg.fdevent.timeout']
|
||||
yield "<h1>Hello World after %s seconds</h1>" % str(time.time() - start_at)
|
||||
@@ -0,0 +1,20 @@
|
||||
import sys
|
||||
|
||||
content_type = 'image/png'
|
||||
filename = 'logo_uWSGI.png'
|
||||
|
||||
try:
|
||||
filename = sys.argv[1]
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
content_type = sys.argv[2]
|
||||
except:
|
||||
pass
|
||||
|
||||
def application(environ, start_response):
|
||||
|
||||
start_response('200 OK', [('Content-Type', content_type)])
|
||||
fd = open(filename,'r')
|
||||
yield environ['wsgi.file_wrapper'](fd, 32*1024)
|
||||
@@ -0,0 +1,456 @@
|
||||
#ifdef UWSGI_UGREEN
|
||||
|
||||
/* uGreen -> uWSGI green threads */
|
||||
|
||||
#include "uwsgi.h"
|
||||
|
||||
#define UGREEN_DEFAULT_STACKSIZE 256*1024
|
||||
|
||||
extern struct uwsgi_server uwsgi;
|
||||
|
||||
void u_green_write_all(struct uwsgi_server *uwsgi, char *data, size_t len) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i;
|
||||
ssize_t rlen ;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
rlen = write(wsgi_req->poll.fd, data, len);
|
||||
if (rlen < 0) {
|
||||
uwsgi_error("write()");
|
||||
// mark core as plagued
|
||||
wsgi_req->async_plagued = 1 ;
|
||||
}
|
||||
else {
|
||||
wsgi_req->response_size += rlen ;
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
|
||||
void u_green_unpause_all(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_PAUSED) {
|
||||
wsgi_req->async_status = UWSGI_AGAIN;
|
||||
wsgi_req->async_timeout = 0;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int u_green_blocking(struct uwsgi_server *uwsgi) {
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status != UWSGI_ACCEPTING && wsgi_req->async_status != UWSGI_PAUSED && wsgi_req->async_waiting_fd == -1 && !wsgi_req->async_timeout) {
|
||||
return 0 ;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
inline static void u_green_schedule_to_main(struct uwsgi_server *uwsgi, int async_id) {
|
||||
|
||||
int py_current_recursion_depth;
|
||||
struct _frame* py_current_frame;
|
||||
|
||||
PyThreadState* tstate = PyThreadState_GET();
|
||||
py_current_recursion_depth = tstate->recursion_depth;
|
||||
py_current_frame = tstate->frame;
|
||||
|
||||
swapcontext(uwsgi->ugreen_contexts[async_id], &uwsgi->ugreenmain);
|
||||
|
||||
tstate = PyThreadState_GET();
|
||||
tstate->recursion_depth = py_current_recursion_depth;
|
||||
tstate->frame = py_current_frame ;
|
||||
}
|
||||
|
||||
inline static void u_green_schedule_to_req(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
|
||||
|
||||
int py_current_recursion_depth;
|
||||
struct _frame* py_current_frame;
|
||||
|
||||
PyThreadState* tstate = PyThreadState_GET();
|
||||
py_current_recursion_depth = tstate->recursion_depth;
|
||||
py_current_frame = tstate->frame;
|
||||
|
||||
uwsgi->wsgi_req = wsgi_req;
|
||||
wsgi_req->async_switches++;
|
||||
swapcontext(&uwsgi->ugreenmain, uwsgi->ugreen_contexts[wsgi_req->async_id] );
|
||||
|
||||
tstate = PyThreadState_GET();
|
||||
tstate->recursion_depth = py_current_recursion_depth;
|
||||
tstate->frame = py_current_frame ;
|
||||
}
|
||||
|
||||
static void u_green_wait_for_fd(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req, int fd, int etype, int timeout) {
|
||||
|
||||
if (fd < 0) return;
|
||||
|
||||
if (async_add(uwsgi->async_queue, fd, etype)) return ;
|
||||
|
||||
wsgi_req->async_waiting_fd = fd ;
|
||||
wsgi_req->async_waiting_fd_type = etype;
|
||||
wsgi_req->async_timeout = time(NULL) + timeout ;
|
||||
|
||||
u_green_schedule_to_main(uwsgi, wsgi_req->async_id);
|
||||
|
||||
async_del(uwsgi->async_queue, wsgi_req->async_waiting_fd, wsgi_req->async_waiting_fd_type);
|
||||
|
||||
wsgi_req->async_waiting_fd = -1;
|
||||
wsgi_req->async_timeout = 0 ;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_pause(PyObject * self, PyObject * args) {
|
||||
|
||||
int timeout = 0 ;
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i", &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wsgi_req->async_status = UWSGI_PAUSED;
|
||||
if (timeout) {
|
||||
wsgi_req->async_timeout = time(NULL) + timeout ;
|
||||
}
|
||||
u_green_schedule_to_main(&uwsgi, wsgi_req->async_id);
|
||||
|
||||
// something bad happened during pause ?
|
||||
if (wsgi_req->async_plagued) {
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_write_all(PyObject * self, PyObject * args) {
|
||||
|
||||
char *data;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &data)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u_green_write_all(&uwsgi, data, strlen(data));
|
||||
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_unpause_all(PyObject * self, PyObject * args) {
|
||||
|
||||
if (uwsgi.async > 1) {
|
||||
u_green_unpause_all(&uwsgi);
|
||||
}
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
|
||||
PyObject *py_uwsgi_green_schedule(PyObject * self, PyObject * args) {
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
|
||||
u_green_schedule_to_main(&uwsgi, wsgi_req->async_id);
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_wait_fdread(PyObject * self, PyObject * args) {
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
int fd,timeout;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i|i", &fd, &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u_green_wait_for_fd(&uwsgi, wsgi_req, fd, ASYNC_IN, timeout);
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_wait_fdwrite(PyObject * self, PyObject * args) {
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
int fd,timeout;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i|i", &fd, &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u_green_wait_for_fd(&uwsgi, wsgi_req, fd, ASYNC_OUT, timeout);
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
PyObject *py_uwsgi_green_sleep(PyObject * self, PyObject * args) {
|
||||
|
||||
struct wsgi_request *wsgi_req = current_wsgi_req(&uwsgi);
|
||||
int timeout;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i", &timeout)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
wsgi_req->async_timeout = time(NULL) + timeout ;
|
||||
u_green_schedule_to_main(&uwsgi, wsgi_req->async_id);
|
||||
wsgi_req->async_timeout = 0 ;
|
||||
|
||||
Py_INCREF(Py_True);
|
||||
return Py_True;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PyMethodDef uwsgi_green_methods[] = {
|
||||
{"green_schedule", py_uwsgi_green_schedule, METH_VARARGS, ""},
|
||||
{"green_wait_fdread", py_uwsgi_green_wait_fdread, METH_VARARGS, ""},
|
||||
{"green_wait_fdwrite", py_uwsgi_green_wait_fdwrite, METH_VARARGS, ""},
|
||||
{"green_sleep", py_uwsgi_green_sleep, METH_VARARGS, ""},
|
||||
{"green_pause", py_uwsgi_green_pause, METH_VARARGS, ""},
|
||||
{"green_unpause_all", py_uwsgi_green_unpause_all, METH_VARARGS, ""},
|
||||
{"green_write_all", py_uwsgi_green_write_all, METH_VARARGS, ""},
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static struct wsgi_request *find_first_accepting_wsgi_req(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_status == UWSGI_ACCEPTING) {
|
||||
return wsgi_req ;
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
return NULL ;
|
||||
}
|
||||
|
||||
|
||||
static void u_green_request(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req, int async_id) {
|
||||
|
||||
|
||||
for(;;) {
|
||||
wsgi_req_setup(wsgi_req, async_id);
|
||||
|
||||
wsgi_req->async_status = UWSGI_ACCEPTING;
|
||||
|
||||
u_green_schedule_to_main(uwsgi, async_id);
|
||||
|
||||
if (wsgi_req_accept(uwsgi->serverfd, wsgi_req)) {
|
||||
continue;
|
||||
}
|
||||
wsgi_req->async_status = UWSGI_OK;
|
||||
|
||||
|
||||
u_green_schedule_to_main(uwsgi, async_id);
|
||||
|
||||
if (wsgi_req_recv(wsgi_req)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
while(wsgi_req->async_status == UWSGI_AGAIN) {
|
||||
u_green_schedule_to_main(uwsgi, async_id);
|
||||
wsgi_req->async_status = (*uwsgi->shared->hooks[wsgi_req->uh.modifier1]) (uwsgi, wsgi_req);
|
||||
}
|
||||
|
||||
u_green_schedule_to_main(uwsgi, async_id);
|
||||
|
||||
uwsgi_close_request(uwsgi, wsgi_req);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void u_green_init(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
|
||||
int i;
|
||||
size_t u_stack_size = UGREEN_DEFAULT_STACKSIZE ;
|
||||
|
||||
|
||||
PyMethodDef *uwsgi_function;
|
||||
|
||||
if (uwsgi->ugreen_stackpages > 0) {
|
||||
u_stack_size = uwsgi->ugreen_stackpages * uwsgi->page_size ;
|
||||
}
|
||||
|
||||
uwsgi_log("initializing %d uGreen threads with stack size of %lu (%lu KB)\n", uwsgi->async, (unsigned long) u_stack_size, (unsigned long) u_stack_size/1024);
|
||||
|
||||
|
||||
uwsgi->ugreen_contexts = malloc( sizeof(ucontext_t*) * uwsgi->async);
|
||||
if (!uwsgi->ugreen_contexts) {
|
||||
uwsgi_error("malloc()\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
uwsgi->ugreen_contexts[i] = malloc( sizeof(ucontext_t) );
|
||||
if (!uwsgi->ugreen_contexts[i]) {
|
||||
uwsgi_error("malloc()");
|
||||
exit(1);
|
||||
}
|
||||
getcontext(uwsgi->ugreen_contexts[i]);
|
||||
uwsgi->ugreen_contexts[i]->uc_stack.ss_sp = mmap(NULL, u_stack_size + (uwsgi->page_size*2) , PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0) + uwsgi->page_size;
|
||||
|
||||
if (!uwsgi->ugreen_contexts[i]->uc_stack.ss_sp) {
|
||||
uwsgi_error("mmap()");
|
||||
exit(1);
|
||||
}
|
||||
// set guard pages for stack
|
||||
if (mprotect(uwsgi->ugreen_contexts[i]->uc_stack.ss_sp - uwsgi->page_size, uwsgi->page_size, PROT_NONE)) {
|
||||
uwsgi_error("mprotect()");
|
||||
exit(1);
|
||||
}
|
||||
if (mprotect(uwsgi->ugreen_contexts[i]->uc_stack.ss_sp + u_stack_size, uwsgi->page_size, PROT_NONE)) {
|
||||
uwsgi_error("mprotect()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
uwsgi->ugreen_contexts[i]->uc_stack.ss_size = u_stack_size ;
|
||||
uwsgi->ugreen_contexts[i]->uc_link = NULL;
|
||||
makecontext(uwsgi->ugreen_contexts[i], (void (*) (void)) &u_green_request, 3, uwsgi, wsgi_req, i);
|
||||
wsgi_req->async_status = UWSGI_ACCEPTING;
|
||||
wsgi_req->async_id = i;
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
for (uwsgi_function = uwsgi_green_methods; uwsgi_function->ml_name != NULL; uwsgi_function++) {
|
||||
PyObject *func = PyCFunction_New(uwsgi_function, NULL);
|
||||
PyDict_SetItemString(uwsgi->embedded_dict, uwsgi_function->ml_name, func);
|
||||
Py_DECREF(func);
|
||||
}
|
||||
}
|
||||
|
||||
void u_green_expire_timeouts(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
time_t deadline = time(NULL);
|
||||
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_timeout > 0) {
|
||||
if (wsgi_req->async_timeout <= deadline) {
|
||||
wsgi_req->async_status = UWSGI_AGAIN ;
|
||||
wsgi_req->async_timeout = 0 ;
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
}
|
||||
|
||||
static int u_green_get_timeout(struct uwsgi_server *uwsgi) {
|
||||
|
||||
|
||||
struct wsgi_request* wsgi_req = uwsgi->wsgi_requests ;
|
||||
int i ;
|
||||
time_t curtime, tdelta = 0 ;
|
||||
int ret = 0 ;
|
||||
|
||||
if (!uwsgi->async_running) return 0;
|
||||
|
||||
for(i=0;i<uwsgi->async;i++) {
|
||||
if (wsgi_req->async_timeout > 0) {
|
||||
if (tdelta <= 0 || tdelta > wsgi_req->async_timeout) {
|
||||
tdelta = wsgi_req->async_timeout ;
|
||||
}
|
||||
}
|
||||
wsgi_req = next_wsgi_req(uwsgi, wsgi_req) ;
|
||||
}
|
||||
|
||||
curtime = time(NULL);
|
||||
|
||||
ret = tdelta - curtime ;
|
||||
if (ret > 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void u_green_loop(struct uwsgi_server *uwsgi) {
|
||||
|
||||
struct wsgi_request *wsgi_req = uwsgi->wsgi_requests ;
|
||||
|
||||
int i, current = 0, timeout ;
|
||||
|
||||
while(uwsgi->workers[uwsgi->mywid].manage_next_request) {
|
||||
|
||||
|
||||
uwsgi->async_running = u_green_blocking(uwsgi) ;
|
||||
timeout = u_green_get_timeout(uwsgi);
|
||||
uwsgi->async_nevents = async_wait(uwsgi->async_queue, uwsgi->async_events, uwsgi->async, uwsgi->async_running, timeout);
|
||||
u_green_expire_timeouts(uwsgi);
|
||||
|
||||
|
||||
if (uwsgi->async_nevents < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
for(i=0; i<uwsgi->async_nevents;i++) {
|
||||
|
||||
if (uwsgi->async_events[i].ASYNC_FD == uwsgi->serverfd) {
|
||||
wsgi_req = find_first_accepting_wsgi_req(uwsgi);
|
||||
if (!wsgi_req) goto cycle;
|
||||
u_green_schedule_to_req(uwsgi, wsgi_req);
|
||||
}
|
||||
else {
|
||||
wsgi_req = find_wsgi_req_by_fd(uwsgi, uwsgi->async_events[i].ASYNC_FD, -1);
|
||||
if (wsgi_req) {
|
||||
u_green_schedule_to_req(uwsgi, wsgi_req);
|
||||
}
|
||||
else {
|
||||
async_del(uwsgi->async_queue, uwsgi->async_events[i].ASYNC_FD, uwsgi->async_events[i].ASYNC_EV);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cycle:
|
||||
|
||||
wsgi_req = find_wsgi_req_by_id(uwsgi, current) ;
|
||||
if (wsgi_req->async_status != UWSGI_ACCEPTING && wsgi_req->async_status != UWSGI_PAUSED && wsgi_req->async_waiting_fd == -1 && !wsgi_req->async_timeout) {
|
||||
u_green_schedule_to_req(uwsgi, wsgi_req);
|
||||
}
|
||||
current++;
|
||||
if (current >= uwsgi->async) current = 0;
|
||||
|
||||
}
|
||||
|
||||
if (uwsgi->workers[uwsgi->mywid].manage_next_request == 0) {
|
||||
reload_me();
|
||||
}
|
||||
else {
|
||||
goodbye_cruel_world();
|
||||
}
|
||||
|
||||
// never here
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Executable
+45
@@ -0,0 +1,45 @@
|
||||
#!./uwsgi
|
||||
import uwsgi
|
||||
|
||||
from bottle import route, default_app, request, response, redirect
|
||||
|
||||
@route('/')
|
||||
def chat():
|
||||
return """<iframe src="/recv"></iframe>
|
||||
<iframe src="/sender"></iframe>"""
|
||||
|
||||
@route('/sender')
|
||||
def sender():
|
||||
return """<form method="GET" action="/send">
|
||||
<textarea name="message"></textarea><br/>
|
||||
<input type="submit" value="send" />
|
||||
</form>"""
|
||||
|
||||
|
||||
|
||||
@route('/recv')
|
||||
def recv():
|
||||
response.header['Transfer-Encoding'] = 'chunked'
|
||||
|
||||
# this will flush headers
|
||||
yield ""
|
||||
|
||||
running = True
|
||||
while running:
|
||||
# this will put the core in pause (for max 10 seconds) and remove it from the sched queue, so the /send can write to its socket
|
||||
if not uwsgi.green_pause(10):
|
||||
running = False
|
||||
# this will maintain the connection opened if no data arrives
|
||||
yield "<span/>"
|
||||
|
||||
|
||||
@route('/send')
|
||||
def send():
|
||||
# this will write to all the core in PAUSED state
|
||||
uwsgi.green_write_all(request.GET['message'] + "<br/>")
|
||||
# this will unpause all the paused cores, so they will be re-inserted in the sched queue
|
||||
uwsgi.green_unpause_all()
|
||||
redirect('/sender')
|
||||
|
||||
|
||||
application = default_app()
|
||||
@@ -1,50 +1,50 @@
|
||||
/* taken from http://wiki.unbit.it/UnbitKernel */
|
||||
struct uidsec_struct {
|
||||
/* network limits */
|
||||
unsigned short ipv4_tcp_port;
|
||||
unsigned short ipv4_udp_port;
|
||||
unsigned short ipv4_tcp_control_port;
|
||||
/* network limits */
|
||||
unsigned short ipv4_tcp_port;
|
||||
unsigned short ipv4_udp_port;
|
||||
unsigned short ipv4_tcp_control_port;
|
||||
|
||||
int ipv4_tcp_socket_protection;
|
||||
unsigned long long ipv4_firewall_mask;
|
||||
int ipv4_tcp_socket_protection;
|
||||
unsigned long long ipv4_firewall_mask;
|
||||
|
||||
/* limits */
|
||||
int fs_readonly;
|
||||
/* limits */
|
||||
int fs_readonly;
|
||||
|
||||
/* process markers */
|
||||
int domain_id;
|
||||
int fcgi_id;
|
||||
int scgi_id;
|
||||
int apps_id;
|
||||
int wapp_id;
|
||||
int ssh_id;
|
||||
int cron_id;
|
||||
/* process markers */
|
||||
int domain_id;
|
||||
int fcgi_id;
|
||||
int scgi_id;
|
||||
int apps_id;
|
||||
int wapp_id;
|
||||
int ssh_id;
|
||||
int cron_id;
|
||||
|
||||
/* thread ?*/
|
||||
int cloned_vm;
|
||||
/* thread ? */
|
||||
int cloned_vm;
|
||||
|
||||
/* process limit */
|
||||
int max_dom_proc;
|
||||
int max_apps_proc;
|
||||
int max_apps_thread;
|
||||
int max_ssh_proc;
|
||||
int max_cron_proc;
|
||||
/* process limit */
|
||||
int max_dom_proc;
|
||||
int max_apps_proc;
|
||||
int max_apps_thread;
|
||||
int max_ssh_proc;
|
||||
int max_cron_proc;
|
||||
|
||||
/* misc */
|
||||
int control_uid;
|
||||
int listen_backlog;
|
||||
int errors ;
|
||||
/* misc */
|
||||
int control_uid;
|
||||
int listen_backlog;
|
||||
int errors;
|
||||
|
||||
/* counters */
|
||||
int accept_cnt ;
|
||||
int accept_last;
|
||||
/* counters */
|
||||
int accept_cnt;
|
||||
int accept_last;
|
||||
|
||||
int fork_cnt ;
|
||||
int fork_last;
|
||||
int fork_cnt;
|
||||
int fork_last;
|
||||
|
||||
unsigned long long bio_read;
|
||||
unsigned long long bio_write;
|
||||
unsigned long long bio_read;
|
||||
unsigned long long bio_write;
|
||||
|
||||
int memory_errors;
|
||||
int memory_errors;
|
||||
|
||||
};
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import uwsgi
|
||||
|
||||
def ciao():
|
||||
uwsgi.start_response('200 OK', [ ('Content-type', 'text/plain') ])
|
||||
uwsgi.send(str(22+30))
|
||||
|
||||
def application(env, start_response):
|
||||
|
||||
start_response('200 Ok', [('Content-type', 'text/html')])
|
||||
|
||||
if env['REQUEST_METHOD'] == 'POST':
|
||||
print "getting file..."
|
||||
remains = int(env['CONTENT_LENGTH'])
|
||||
print remains
|
||||
buf = ''
|
||||
while remains > 0:
|
||||
if remains < 8192:
|
||||
buf = env['wsgi.input'].read(remains)
|
||||
else:
|
||||
buf = env['wsgi.input'].read(8192)
|
||||
#print len(buf)
|
||||
remains = remains - len(buf)
|
||||
#print "remains",remains
|
||||
print "upload ready"
|
||||
yield env['CONTENT_LENGTH']
|
||||
else:
|
||||
yield """
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
<input type="file" name="file" />
|
||||
<input type="submit" value="invia" />
|
||||
</form>
|
||||
"""
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user