(headers, content)
| 120 | } |
| 121 | |
| 122 | function handleRequest(headers, content) { |
| 123 | var methodEnd = headers.indexOf(" "); |
| 124 | var uriEnd = headers.indexOf(" ", methodEnd + 1); |
| 125 | var method = headers.substring(0, methodEnd); |
| 126 | var uri = headers.substring(methodEnd + 1, uriEnd); |
| 127 | var R = /^Origin: chrome-extension:\/\/hifhgpdkfodlpnlmlnmhchnkepplebkb\r?$/; |
| 128 | var valid = headers.split('\n').some(function(header) { |
| 129 | return !!header.match(R); |
| 130 | }); |
| 131 | |
| 132 | if (method == 'GET') { |
| 133 | // handle "hello" requests |
| 134 | if (uri == '/hello') { |
| 135 | sendResponse(socketId, 'Hello'); |
| 136 | } else { |
| 137 | sendResponse(socketId, 'I have no idea what you\'re talking about'); |
| 138 | } |
| 139 | socket.accept(socketInfo.socketId, onAccept); |
| 140 | } else |
| 141 | |
| 142 | if (method == 'POST' && valid) { |
| 143 | |
| 144 | // parse post data |
| 145 | if (content) { |
| 146 | query = parseQuery(content); |
| 147 | } else { |
| 148 | query = {}; |
| 149 | } |
| 150 | |
| 151 | try { |
| 152 | // handle "start" requests |
| 153 | if (uri == '/start') { |
| 154 | // start requests need 'request' (unique) and 'type' properties |
| 155 | if (!query.hasOwnProperty('request')) { |
| 156 | sendResponse(socketId, |
| 157 | '{"error": "INVALID START: request not given"}'); |
| 158 | } else if (!query.hasOwnProperty('type')) { |
| 159 | sendResponse(socketId, |
| 160 | '{"error": "INVALID START: type not given"}'); |
| 161 | } else if (requests.hasOwnProperty(query.request)) { |
| 162 | sendResponse(socketId, |
| 163 | '{"error": "INVALID START: duplicate request"}'); |
| 164 | } else { |
| 165 | // fire off the start request |
| 166 | startRequest(socketId, query.request, query.type, query.data, |
| 167 | query.aux); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // handle "end" requests |
| 172 | else if (uri == '/end') { |
| 173 | // end requests need already started 'request' property |
| 174 | if (!query.hasOwnProperty('request') || |
| 175 | !requests.hasOwnProperty(query.request) || |
| 176 | requests[query.request].completed) { |
| 177 | sendResponse(socketId, |
| 178 | '{"error": "INVALD END: request not found"}'); |
| 179 | } else { |
no test coverage detected