Contact: fumanchu@aminus.org

Log in as guest/misc to create tickets

root/httprepl.html

Revision 61 (checked in by fumanchu, 7 years ago)

Bah. Bug in first prompt.

Line 
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2     "http://www.w3.org/TR/xhtml1/DTD/strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5     <title>CherryPy Terminal</title>
6
7 <style type='text/css'>
8
9 #output pre {
10     font: 10pt monospace;
11     margin: 0px;
12     padding: 1px;
13     background-color: #F0F0FF;
14    
15     white-space: -moz-pre-wrap; /* Mozilla, supported since 1999 */
16     white-space: -pre-wrap; /* Opera 4 - 6 */
17     white-space: -o-pre-wrap; /* Opera 7 */
18     white-space: pre-wrap; /* CSS3 - Text module (Candidate Recommendation)
19                             http://www.w3.org/TR/css3-text/#white-space */
20     word-wrap: break-word; /* IE 5.5+ */
21 }
22
23 #output pre.stdin {
24     border-top: 1px solid #E0E0FF;
25 }
26
27 form {
28     font: 10pt monospace;
29     margin: 0;
30 }
31
32 #input {
33     font: 10pt monospace;
34 }
35
36 #inputsize {
37     border-top: none;
38     border-right: none;
39     border-bottom: 2px inset;
40     border-left: none;
41     text-align: right;
42 }
43
44 </style>
45
46 <script type='text/javascript'>
47
48 function http() {
49     var h;
50     if (typeof(XMLHttpRequest) != "undefined") {
51         h = new XMLHttpRequest();
52     } else {
53         try { h = new ActiveXObject("Msxml2.XMLHTTP"); }
54         catch (e) {
55             try { h = new ActiveXObject("Microsoft.XMLHTTP"); }
56             catch (E) { alert("Your browser is not supported."); }
57         }
58     }
59     return h
60 }
61
62 function use_history(elem) {
63     var content = get_text(elem);
64     // Strip the leading prompt
65     if (elem.className == 'stdin') content = content.substr(4);
66     input.value = content;
67     input.focus();
68 }
69
70 function history_hook() {
71     use_history(this);
72 }
73
74 var current_history = undefined;
75
76 function prev_history() {
77     if (current_history == undefined) {
78         var prev = document.getElementById("output").lastChild;
79     } else {
80         var prev = current_history.previousSibling;
81     }
82     while (prev != null && prev.className != 'stdin') {
83         prev = prev.previousSibling;
84     }
85     if (prev != null) {
86         current_history = prev;
87         use_history(prev);
88     }
89 }
90
91 function next_history() {
92     if (current_history == undefined) return;
93    
94     var next = current_history.nextSibling;
95     while (next != null && next.className != 'stdin') {
96         next = next.nextSibling;
97     }
98     if (next != null) {
99         current_history = next;
100         use_history(next);
101     }
102 }
103
104 function mapKeys(key_code) {
105     switch(key_code) {
106     case 38:   // up arrow
107         prev_history();
108         break;
109     case 40:   // down arrow
110         next_history();
111         break;
112     }
113 }
114
115 function trapKeys(e) {
116     if (!e) var e = window.event;
117     mapKeys(e.keyCode);
118 }
119 document.onkeyup = trapKeys;
120
121
122 function stdout_write(content, stdin) {
123     current_history = undefined;
124     if (content == "") return;
125    
126     var block = document.createElement("pre");
127     block.ondblclick = history_hook;
128     if (stdin) {
129         content = get_prompt() + content;
130         block.className = 'stdin';
131     }
132     if (block.innerText != undefined) {
133         // Internet Explorer quirkiness
134         block.innerText = content;
135     } else {
136         block.appendChild(document.createTextNode(content));
137     }
138     output.appendChild(block);
139 }
140
141 function get_text(elem) {
142     if (elem.innerText != undefined) {
143         // Internet Explorer
144         return elem.innerText;
145     } else {
146         // Mozilla
147         return elem.textContent;
148     }
149 }
150
151 function get_prompt() {
152     return get_text(document.getElementById("prompt"));
153 }
154
155 function set_prompt(newvalue) {
156     var p = document.getElementById("prompt");
157     if (p.innerText != undefined) {
158         // Internet Explorer
159         p.innerText = newvalue;
160     } else {
161         // Mozilla
162         p.textContent = newvalue;
163     }
164 }
165
166 function push() {
167     var data = input.value;
168     input.value = "";
169    
170     var h = http();
171     h.onreadystatechange = function() {
172         if (h.readyState == 4) {
173             stdout_write(data, true);
174             try {
175                 var status = h.status;
176             } catch(e) {
177                 var status = "NO HTTP RESPONSE";
178             }
179             switch (status) {
180                 case 200:
181                     set_prompt(">>> ");
182                     stdout_write(h.responseText, false);
183                     break;
184                 // Internet Explorer might return 1223 for 204
185                 case 1223:
186                 case 204:
187                     set_prompt("... ");
188                     break;
189                 case 12029:
190                     // Internet Explorer client could not connect to server
191                     status = "NO HTTP RESPONSE";
192                 default:
193                     stdout_write(status + "\n" + h.responseText, false);
194             }
195             input.focus();
196         }
197     }
198     h.open("POST", "ajax_push", true);
199     h.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
200     h.send("line=" + escape(data));
201 }
202
203 function escape(data) {
204     data = data.replace(/%/gi, "%25");
205     data = data.replace(/&/gi, "%26");
206     data = data.replace(/[+]/gi, "%2B");
207     data = data.replace(/;/gi, "%3B");
208     return data;
209 }
210
211 function resize_input(newsize) {
212     newsize = parseInt(newsize);
213     if (newsize > 0) {
214         document.getElementById('input').size = newsize;
215     }
216 }
217
218 input = undefined;
219 output = undefined;
220
221 function init() {
222     input = document.getElementById("input");
223     output = document.getElementById("output");
224     input.focus();
225 }
226
227 </script>
228 </head>
229
230 <body onload="init();">
231 <h1>CherryPy Terminal</h1>
232
233 <div id="output">
234 </div>
235
236 <form action="" id="terminal" onsubmit="push(); return false;">
237 <nobr><span id='prompt'>&gt;&gt;&gt; </span><input type="text" id="input" size="60" />
238 <input type="submit" value="submit" />
239 <input type="text" value="60" size="3" id="inputsize"
240     onkeyup="resize_input(this.value)" /></nobr>
241 </form>
242
243 </body>
244 </html>
Note: See TracBrowser for help on using the browser.