Check-in [c30e7bfb66]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Mainly added some debug output code in order to diagnose problems with KolibriOS port.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:c30e7bfb6602b915a3085c99d5601436b4ead5bc
User & Date: johnfound 2015-12-10 14:36:43
Context
2015-12-17
15:21
Changed a little the error handling of SocketAccept. Leaf check-in: 5e1458a965 user: johnfound tags: trunk
2015-12-10
14:36
Mainly added some debug output code in order to diagnose problems with KolibriOS port. check-in: c30e7bfb66 user: johnfound tags: trunk
2015-12-04
23:12
The project license has been changed to BSD 2-clause license. check-in: 75d3e16e45 user: johnfound tags: trunk
Changes

Changes to phWeb/phWeb.asm.

1
2
3
4
5
6
7
8
9
10
11
..
50
51
52
53
54
55
56


57
58







59
60
61



62
63
64



65
66
67
68
69
70
71
72
73
74
75
76
..
77
78
79
80
81
82
83










84
85
86
87
88
89
90
..
94
95
96
97
98
99
100


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
include "%lib%/freshlib.inc"
@BinaryType console

options.DebugMode = 0
options.ShowSizes = 1

match =Linux, TargetOS {
  HeapManager equ ASM
  LinuxThreads equ native
}

................................................................................
        stdcall TerminateAll, 0

; ================== Loading config file ===================

; procedure load and parse configuration data
proc LoadConfiguration
begin


        pushad








        stdcall CfgGetInt, cDefaultConfig, 'Binding', 'BindPort', 8080
        mov     [cfgBindPort], eax




        stdcall CfgGetInt, cDefaultConfig, 'Binding', 'BindIP', $7f000001
        mov     [cfgBindIp], eax




        stdcall CfgGetStr, cDefaultConfig, 'WebSite', 'DocumentRoot'
        jnc     .save_root

        stdcall StrNew
        stdcall StrCharCat, eax, '.'

.save_root:
        mov     [cfgRoot], eax
        stdcall StrClipSpacesR, [cfgRoot]
        stdcall StrClipSpacesL, [cfgRoot]

        stdcall StrLen, [cfgRoot]
................................................................................
        mov     ecx, eax
        stdcall StrPtr, [cfgRoot]
        cmp     byte [eax+ecx-1], DIR_SLASH
        je      .root_ok
        stdcall StrCharCat, [cfgRoot], DIR_SLASH
.root_ok:











        popad
        return
endp


; ------------------------------------------------------------

................................................................................
  cfgBindIp      dd ?
  cfgBindPort    dd ?
  cfgRoot        dd ?

; not implemented.
  cfgDefaultFiles dd ?          ; pointer to an array with default filenames sorted by priority.
  cfgCGIFileTypes dd ?          ; pointer to an array with file type information - i.e. the file extension and how to process it.


endg


; Strings constants

strStartMsg     text  'Starting up server.  Bind to: '
exit_msg        text  'Exiting...', 13, 10

cCRLF           text  13, 10, 0

cDefaultConfig  text  './phWeb.conf'  ; default config file. The command line option can override this.
cDefaultRoot    text  "./"
cDefaultFile    text  "index.html"






|







 







>
>


>
>
>
>
>
>
>
|


>
>
>
|


>
>
>
|


|
|







 







>
>
>
>
>
>
>
>
>
>







 







>
>










|
<




1
2
3
4
5
6
7
8
9
10
11
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
..
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

139
140
141
142
include "%lib%/freshlib.inc"
@BinaryType console

options.DebugMode = 1
options.ShowSizes = 1

match =Linux, TargetOS {
  HeapManager equ ASM
  LinuxThreads equ native
}

................................................................................
        stdcall TerminateAll, 0

; ================== Loading config file ===================

; procedure load and parse configuration data
proc LoadConfiguration
begin
        DebugMsg "Load configuration."

        pushad

        stdcall GetCurrentDir
        mov     [hConfigFile], eax

        stdcall StrCharCat, eax, DIR_SLASH
        stdcall StrCat, eax, cDefaultConfig


        stdcall CfgGetInt, [hConfigFile], 'Binding', 'BindPort', 8080
        mov     [cfgBindPort], eax

        OutputValue "Bind port:", eax, 10, -1


        stdcall CfgGetInt, [hConfigFile], 'Binding', 'BindIP', $7f000001
        mov     [cfgBindIp], eax

        OutputValue "Bind IP: $", eax, 16, 8


        stdcall CfgGetStr, [hConfigFile], 'WebSite', 'DocumentRoot'
        jnc     .save_root

        stdcall GetCurrentDir
        stdcall StrCharCat, eax, DIR_SLASH

.save_root:
        mov     [cfgRoot], eax
        stdcall StrClipSpacesR, [cfgRoot]
        stdcall StrClipSpacesL, [cfgRoot]

        stdcall StrLen, [cfgRoot]
................................................................................
        mov     ecx, eax
        stdcall StrPtr, [cfgRoot]
        cmp     byte [eax+ecx-1], DIR_SLASH
        je      .root_ok
        stdcall StrCharCat, [cfgRoot], DIR_SLASH
.root_ok:

  if defined options.DebugMode & options.DebugMode
        stdcall Output, 'Documents root: "'

        stdcall StrPtr, [cfgRoot]
        stdcall Output, eax
        stdcall Output, cCRLF
  end if


        DebugMsg "Load configuration done."
        popad
        return
endp


; ------------------------------------------------------------

................................................................................
  cfgBindIp      dd ?
  cfgBindPort    dd ?
  cfgRoot        dd ?

; not implemented.
  cfgDefaultFiles dd ?          ; pointer to an array with default filenames sorted by priority.
  cfgCGIFileTypes dd ?          ; pointer to an array with file type information - i.e. the file extension and how to process it.

  hConfigFile   dd ?
endg


; Strings constants

strStartMsg     text  'Starting up server.  Bind to: '
exit_msg        text  'Exiting...', 13, 10

cCRLF           text  13, 10, 0

cDefaultConfig  text  'phWeb.conf'  ; default config file. The command line option can override this.

cDefaultFile    text  "index.html"



Changes to phWeb/phWeb.conf.

1
2
3
4
Binding.BindIP   = $7f000001
Binding.BindPort = 8000

WebSite.DocumentRoot = "./www"
|



1
2
3
4
Binding.BindIP	 = $7f000001
Binding.BindPort = 8000

WebSite.DocumentRoot = "./www"

Changes to phWeb/phWeb.fpr.

cannot compute difference between binary files

Changes to phWeb/sockets.asm.

8
9
10
11
12
13
14




15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

59
60
61



62
63
64
65
66
67
68
69
70
71
72


73
74
75
76
77
78
79
80

81
82
83
84

85
86
87
88

89
90
91
92

93
94
95
96
97
98
99
...
217
218
219
220
221
222
223
224
225
226
227
228

229

230
231
232
233
234
235
236
237
238
239
.hSocket dd ?
begin
        stdcall SocketCreate, PF_INET, SOCK_STREAM, 0
        jc      .sockerr

        mov     [.hSocket], eax     ; save socket handle





; Bind the socket to the address

; fillup sockaddr
        lea     ecx, [.addr]

        mov     [ecx+TSocketAddressIn.saFamily], AF_INET
        mov     eax, [cfgBindIp]
        mov     edx, [cfgBindPort]
        bswap   eax
        xchg    dl, dh
        mov     [ecx+TSocketAddressIn.saAddress], eax
        mov     [ecx+TSocketAddressIn.saPort], dx

        OutputRegister regEDX, 16

        stdcall  SocketBind, [.hSocket], ecx
        jc       .binderr


; Set the socket to listen for the incomming connections.
.doListen:


        stdcall SocketListen, [.hSocket], 1024
        jc      .listenerr

        DebugMsg "Socket listen OK"
        OutputRegister regEAX, 16

; Accept connection
.doAccept:
        DebugMsg "SocketAccept shoud block now."

        stdcall  SocketAccept, [.hSocket], 0
        jc      .accepterr

        mov     ebx, eax

        DebugMsg "Connection accepted."
        OutputRegister regEBX, 16

        stdcall ThreadCreate, ConnectionHandler, ebx
        jc      .thread_error

        stdcall FreeThreadID, eax

        inc     [ThreadCount]

        jmp     .doAccept

.thread_error:



        stdcall  SocketClose, ebx
        jmp     .doAccept


uglobal
  ThreadCount dd ?
endg

; ------------------------------------------------------------

.exit_s:


        stdcall SocketClose, [.hSocket]
        return

; ------------------------------------------------------------

; These are just to print an error message to stderr should
; one of our socket calls fail.
.sockerr:

        stdcall FileWriteString, [STDERR], cSocksBroke
        jmp     .exit_s

.binderr:

        stdcall FileWriteString, [STDERR], cBindsBroke
        jmp     .exit_s

.listenerr:

        stdcall FileWriteString, [STDERR], cListensBroke
        jmp     .exit_s

.accepterr:

        stdcall FileWriteString, [STDERR], cAcceptsBroke
;        jmp     .doAccept

        jmp     .exit_s
endp


................................................................................
        rep movsb               ; copy the rest of the buffer to the begining of the buffer.

        stdcall ProcessRequest, [.hSocket], [.pHeaders], [.buffer], cStreamBufferSize, edi

.finish:
        stdcall FreeMem,     [.buffer]
        stdcall StrDel,      [.hCurrent]
        stdcall ListFree, [.pHeaders], StrDel
        stdcall SocketClose, [.hSocket]

if defined options.DebugMode & options.DebugMode
        jnc     .end_thread

        DebugMsg "Error close socket."

.end_thread:
        DebugMsg "Socket closed."
end if

        dec     [ThreadCount]
        popad
        return
endp









>
>
>
>













<
<


>



>
>




<



|






|
<






|
>



>
>
>




<
<
<
<



>
>








>




>




>




>







 







|




>

>

|


<





8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


32
33
34
35
36
37
38
39
40
41
42
43

44
45
46
47
48
49
50
51
52
53
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72




73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

245
246
247
248
249
.hSocket dd ?
begin
        stdcall SocketCreate, PF_INET, SOCK_STREAM, 0
        jc      .sockerr

        mov     [.hSocket], eax     ; save socket handle

        stdcall SocketSetOption, eax, soReuseAddr, TRUE

        OutputValue "Socket created. Handle = $", [.hSocket], 16, 8

; Bind the socket to the address

; fillup sockaddr
        lea     ecx, [.addr]

        mov     [ecx+TSocketAddressIn.saFamily], AF_INET
        mov     eax, [cfgBindIp]
        mov     edx, [cfgBindPort]
        bswap   eax
        xchg    dl, dh
        mov     [ecx+TSocketAddressIn.saAddress], eax
        mov     [ecx+TSocketAddressIn.saPort], dx



        stdcall  SocketBind, [.hSocket], ecx
        jc       .binderr


; Set the socket to listen for the incomming connections.
.doListen:
        OutputValue "SocketListen. Handle = $", [.hSocket], 16, 8

        stdcall SocketListen, [.hSocket], 1024
        jc      .listenerr

        DebugMsg "Socket listen OK"


; Accept connection
.doAccept:
        OutputValue "SockedAccept Handle = $", [.hSocket], 16, 8

        stdcall  SocketAccept, [.hSocket], 0
        jc      .accepterr

        mov     ebx, eax

        OutputValue "Connection accepted. New handle = $", ebx, 16, 8


        stdcall ThreadCreate, ConnectionHandler, ebx
        jc      .thread_error

        stdcall FreeThreadID, eax

        DebugMsg "Request served."

        jmp     .doAccept

.thread_error:

        DebugMsg "Thread create error."

        stdcall  SocketClose, ebx
        jmp     .doAccept






; ------------------------------------------------------------

.exit_s:
        DebugMsg "Error exit server loop."

        stdcall SocketClose, [.hSocket]
        return

; ------------------------------------------------------------

; These are just to print an error message to stderr should
; one of our socket calls fail.
.sockerr:
        DebugMsg "SocketCreate failed."
        stdcall FileWriteString, [STDERR], cSocksBroke
        jmp     .exit_s

.binderr:
        DebugMsg "SocketBind failed."
        stdcall FileWriteString, [STDERR], cBindsBroke
        jmp     .exit_s

.listenerr:
        DebugMsg "SocketListen failed."
        stdcall FileWriteString, [STDERR], cListensBroke
        jmp     .exit_s

.accepterr:
        DebugMsg "SocketAccept failed."
        stdcall FileWriteString, [STDERR], cAcceptsBroke
;        jmp     .doAccept

        jmp     .exit_s
endp


................................................................................
        rep movsb               ; copy the rest of the buffer to the begining of the buffer.

        stdcall ProcessRequest, [.hSocket], [.pHeaders], [.buffer], cStreamBufferSize, edi

.finish:
        stdcall FreeMem,     [.buffer]
        stdcall StrDel,      [.hCurrent]
        stdcall ListFree,    [.pHeaders], StrDel
        stdcall SocketClose, [.hSocket]

if defined options.DebugMode & options.DebugMode
        jnc     .end_thread

        DebugMsg "Error close socket."

.end_thread:
        OutputValue "Socket closed: $", [.hSocket], 16, 8
end if


        popad
        return
endp