|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* Debug and Error wrapper functions for dump(). |
|
8 */ |
|
9 function ERR(response, responseCode, responseCodeStr, msg) |
|
10 { |
|
11 // Reset state var. |
|
12 setState("expectedRequestType", ""); |
|
13 // Dump to console log and send to client in response. |
|
14 dump("SERVER ERROR: " + msg + "\n"); |
|
15 response.write("HTTP/1.1 " + responseCode + " " + responseCodeStr + "\r\n"); |
|
16 response.write("Content-Type: text/html; charset=UTF-8\r\n"); |
|
17 response.write("Content-Length: " + msg.length + "\r\n"); |
|
18 response.write("\r\n"); |
|
19 response.write(msg); |
|
20 } |
|
21 |
|
22 function DBG(msg) |
|
23 { |
|
24 // Dump to console only. |
|
25 dump("SERVER DEBUG: " + msg + "\n"); |
|
26 } |
|
27 |
|
28 /* Delivers content in parts to test partially cached content: requires two |
|
29 * requests for the same resource. |
|
30 * |
|
31 * First call will respond with partial content, but a 200 header and |
|
32 * Content-Length equal to the full content length. No Range or If-Range |
|
33 * headers are allowed in the request. |
|
34 * |
|
35 * Second call will require Range and If-Range in the request headers, and |
|
36 * will respond with the range requested. |
|
37 */ |
|
38 function handleRequest(request, response) |
|
39 { |
|
40 DBG("Trying to seize power"); |
|
41 response.seizePower(); |
|
42 |
|
43 DBG("About to check state vars"); |
|
44 // Get state var to determine if this is the first or second request. |
|
45 var expectedRequestType; |
|
46 var lastModified; |
|
47 if (getState("expectedRequestType") === "") { |
|
48 DBG("First call: Should be requesting full content."); |
|
49 expectedRequestType = "fullRequest"; |
|
50 // Set state var for second request. |
|
51 setState("expectedRequestType", "partialRequest"); |
|
52 // Create lastModified variable for responses. |
|
53 lastModified = (new Date()).toUTCString(); |
|
54 setState("lastModified", lastModified); |
|
55 } else if (getState("expectedRequestType") === "partialRequest") { |
|
56 DBG("Second call: Should be requesting undelivered content."); |
|
57 expectedRequestType = "partialRequest"; |
|
58 // Reset state var for first request. |
|
59 setState("expectedRequestType", ""); |
|
60 // Get last modified date and reset state var. |
|
61 lastModified = getState("lastModified"); |
|
62 } else { |
|
63 ERR(response, 500, "Internal Server Error", |
|
64 "Invalid expectedRequestType \"" + expectedRequestType + "\"in " + |
|
65 "server state db."); |
|
66 return; |
|
67 } |
|
68 |
|
69 // Look for Range and If-Range |
|
70 var range = request.hasHeader("Range") ? request.getHeader("Range") : ""; |
|
71 var ifRange = request.hasHeader("If-Range") ? request.getHeader("If-Range") : ""; |
|
72 |
|
73 if (expectedRequestType === "fullRequest") { |
|
74 // Should not have Range or If-Range in first request. |
|
75 if (range && range.length > 0) { |
|
76 ERR(response, 400, "Bad Request", |
|
77 "Should not receive \"Range: " + range + "\" for first, full request."); |
|
78 return; |
|
79 } |
|
80 if (ifRange && ifRange.length > 0) { |
|
81 ERR(response, 400, "Bad Request", |
|
82 "Should not receive \"Range: " + range + "\" for first, full request."); |
|
83 return; |
|
84 } |
|
85 } else if (expectedRequestType === "partialRequest") { |
|
86 // Range AND If-Range should both be present in second request. |
|
87 if (!range) { |
|
88 ERR(response, 400, "Bad Request", |
|
89 "Should receive \"Range: \" for second, partial request."); |
|
90 return; |
|
91 } |
|
92 if (!ifRange) { |
|
93 ERR(response, 400, "Bad Request", |
|
94 "Should receive \"If-Range: \" for second, partial request."); |
|
95 return; |
|
96 } |
|
97 } else { |
|
98 // Somewhat redundant, but a check for errors in this test code. |
|
99 ERR(response, 500, "Internal Server Error", |
|
100 "expectedRequestType not set correctly: \"" + expectedRequestType + "\""); |
|
101 return; |
|
102 } |
|
103 |
|
104 // Prepare content in two parts for responses. |
|
105 var partialContent = "<html><head></head><body><p id=\"firstResponse\">" + |
|
106 "First response</p>"; |
|
107 var remainderContent = "<p id=\"secondResponse\">Second response</p>" + |
|
108 "</body></html>"; |
|
109 var totalLength = partialContent.length + remainderContent.length; |
|
110 |
|
111 DBG("totalLength: " + totalLength); |
|
112 |
|
113 // Prepare common headers for the two responses. |
|
114 date = new Date(); |
|
115 DBG("Date: " + date.toUTCString() + ", Last-Modified: " + lastModified); |
|
116 var commonHeaders = "Date: " + date.toUTCString() + "\r\n" + |
|
117 "Last-Modified: " + lastModified + "\r\n" + |
|
118 "Content-Type: text/html; charset=UTF-8\r\n" + |
|
119 "ETag: abcd0123\r\n" + |
|
120 "Accept-Ranges: bytes\r\n"; |
|
121 |
|
122 |
|
123 // Prepare specific headers and content for first and second responses. |
|
124 if (expectedRequestType === "fullRequest") { |
|
125 DBG("First response: Sending partial content with a full header"); |
|
126 response.write("HTTP/1.1 200 OK\r\n"); |
|
127 response.write(commonHeaders); |
|
128 // Set Content-Length to full length of resource. |
|
129 response.write("Content-Length: " + totalLength + "\r\n"); |
|
130 response.write("\r\n"); |
|
131 response.write(partialContent); |
|
132 } else if (expectedRequestType === "partialRequest") { |
|
133 DBG("Second response: Sending remaining content with a range header"); |
|
134 response.write("HTTP/1.1 206 Partial Content\r\n"); |
|
135 response.write(commonHeaders); |
|
136 // Set Content-Length to length of bytes transmitted. |
|
137 response.write("Content-Length: " + remainderContent.length + "\r\n"); |
|
138 response.write("Content-Range: bytes " + partialContent.length + "-" + |
|
139 (totalLength - 1) + "/" + totalLength + "\r\n"); |
|
140 response.write("\r\n"); |
|
141 response.write(remainderContent); |
|
142 } else { |
|
143 // Somewhat redundant, but a check for errors in this test code. |
|
144 ERR(response, 500, "Internal Server Error", |
|
145 "Something very bad happened here: expectedRequestType is invalid " + |
|
146 "towards the end of handleRequest! - \"" + expectedRequestType + "\""); |
|
147 return; |
|
148 } |
|
149 |
|
150 response.finish(); |
|
151 } |