File: ns_openssl_sockcallback.tcl

package info (click to toggle)
aolserver4-nsopenssl 3.0beta26-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 448 kB
  • ctags: 312
  • sloc: ansic: 3,162; tcl: 1,080; makefile: 161
file content (112 lines) | stat: -rwxr-xr-x 2,879 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# nsopenssl socket testing setup
# Copyright (c) 2001 by Scott S. Goodwin
# See http://scottg.net for more information

ns_log notice "loading test-sockcallback.tcl"

# sockcallback
ns_register_proc GET /do_sockcallback do_sockcallback
proc do_sockcallback {} {
    set sock [ns_socklisten [nsv_get . httpaddr] [nsv_get . listenport]]
    ns_sockcallback $sock handle_sockcallback r
    set content [do_content "<b>tested ns_sockcallback</b><br>"]
    set rc [do_write [do_header $content] $content]
}

# SSL sockcallback
ns_register_proc GET /do_ssl_sockcallback do_ssl_sockcallback
proc do_ssl_sockcallback {} {
    set sock [ns_openssl_socklisten [nsv_get . httpaddr] [nsv_get . listensslport]]
    ns_openssl_sockcallback $sock handle_ssl_sockcallback r
    set content [do_content "<b>tested ns_openssl_sockcallback</b><br>"]
    set rc [do_write [do_header $content] $content]
}

proc handle_sockcallback {sock when} {
    set p "handle_sockcallback"
    ns_log notice "$p: a client has connected to the socket"
    set fds [ns_sockaccept $sock]
    set rfd [lindex $fds 0]
    set wfd [lindex $fds 1]
    ns_log notice "$p: RFD=$rfd; WFD=$wfd"
    while {[set line [string trim [gets $rfd]]] != ""} {
	lappend headers $line
    }
    ns_log notice "$p: CLIENT HTTP HEADERS:"
    ns_log notice "$p: $headers"

    set content_htm \
"<html>
<head>
<title>non-ssl test</title>
</head>
<body>
<p>Great! We were able to do a non-SSL sockcallback, read the client
HTTP header and send back this HTML page. Hit the back button to
return to the test page.
<p>Here ar the HTTP client headers you sent me:
<p>$headers
</body>
</html>"

    set myheader \
        "HTTP/1.0 200 Document follows
MIME-Version: 1.0
Content-Type: text/html
Content-Length: [string length $content_htm]"

    puts $wfd \
"$myheader


$content_htm"

    flush $wfd
    close $rfd
    close $wfd
}

proc handle_ssl_sockcallback {sock when} {
    set p "handle_ssl_sockcallback"
    ns_log notice "$p: a client has connected to the SSL socket"
    set fds [ns_openssl_sockaccept $sock]
    set rfd [lindex $fds 0]
    set wfd [lindex $fds 1]
    while {[set line [string trim [gets $rfd]]] != ""} {
	lappend headers $line
    }
    ns_log notice "$p: CLIENT HTTP HEADERS:"
    ns_log notice "$p: $headers"

    set content_htm \
"<html>
<head>
<title>ssl test</title>
</head>
<body>
<p>Great! We were able to do an SSL sockcallback, read the client
HTTP header and send back this HTML page. Hit the back button to
return to the test page.
<p>Here ar the HTTP client headers you sent me:
<p>$headers
</body>
</html>"

    set myheader \
        "HTTP/1.0 200 Document follows
MIME-Version: 1.0
Content-Type: text/html
Content-Length: [string length $content_htm]"

    puts $wfd \
"$myheader


$content_htm"

    flush $wfd
    close $rfd
    close $wfd
}

ns_log notice "done loading test-sockcallback.tcl"