-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebServer.st
68 lines (53 loc) · 1.7 KB
/
WebServer.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Object subclass: #WebServer
instanceVariableNames: 'root listener bytes'
classVariableNames: ''
poolDictionaries: ''
category: ''
!
!WebServer class methods!
new
^self basicNew initialize
! !
!WebServer methods!
initialize
bytes := @System.Array !!CreateInstance: @System.Byte with: 1024 * 16.
listener := @System.Net.HttpListener !!new.
listener !!Prefixes !!Add: 'http://*:8000/'.
root := 'c:/apache-tomcat-6.0.18/webapps/docs'.
@System.Console !!WriteLine: 'initialize'
!
process: context
| filename input nbytes |
filename := context !!Request !!Url !!AbsolutePath.
@System.Console !!WriteLine: filename.
(filename = '' or: [filename = '/']) ifTrue: [filename := 'index.html'].
(filename !!StartsWith: '/') ifTrue: [filename := filename !!Substring: 1].
@System.Console !!WriteLine: filename.
filename := @System.IO.Path !!Combine: root with: filename.
@System.Console !!WriteLine: filename.
(@System.IO.File !!Exists: filename)
ifFalse: [ context !!Response !!Abort. ]
ifTrue: [
input := @System.IO.FileStream !!new: filename with: @System.IO.FileMode !!Open.
[[nbytes := input !!Read: bytes with: 0 with: bytes !!Length] value > 0] whileTrue: [
context !!Response !!OutputStream !!Write: bytes with: 0 with: nbytes.
].
input !!Close.
context !!Response !!OutputStream !!Close
]
!
start
listener !!Start.
@System.Console !!WriteLine: 'start'.
[true] whileTrue: [
| context |
@System.Console !!WriteLine: 'get context'.
context := listener !!GetContext.
@System.Console !!WriteLine: 'new request'.
self process: context.
].
@System.Console !!WriteLine: 'end start'
! !
WebServer new start
!