| 132 | } |
| 133 | |
| 134 | int httpget::get(const char *url1, uint timeout, uint totaltimeout, int range, bool head) |
| 135 | { |
| 136 | int redirects = 0, res = 0, transferred = 0; |
| 137 | vector<char> *content = NULL; |
| 138 | ASSERT(timeout && totaltimeout); // zero is not allowed |
| 139 | stopwatch to; |
| 140 | to.start(); |
| 141 | uint lastresponse = 0; |
| 142 | reset(1); // clear old response values and url |
| 143 | url = newstring(url1); |
| 144 | |
| 145 | bool reconnected = false, retry = false; |
| 146 | for(;;) |
| 147 | { // (in a loop because of possible redirects) |
| 148 | if(!connect()) goto geterror; |
| 149 | |
| 150 | // assemble GET request |
| 151 | cvecprintf(rawsnd, "%s %s HTTP/1.1%s", head ? "HEAD" : "GET", url, crlf); |
| 152 | cvecprintf(rawsnd, "Host: %s%s", hostname, crlf); |
| 153 | cvecprintf(rawsnd, "Accept: */*%s", crlf); |
| 154 | if(referrer) cvecprintf(rawsnd, "Referer: %s%s", referrer, crlf); |
| 155 | if(useragent) cvecprintf(rawsnd, "User-Agent: %s%s", useragent, crlf); |
| 156 | if(range > 0) cvecprintf(rawsnd, "Range: bytes=%d-%sTE: gzip%s", range, crlf, crlf); // content-encoding gzip for range requests is nonsense, so we'll ask for transfer-encoding instead (not that any server will deliver that...) |
| 157 | else if(!head) cvecprintf(rawsnd, "Accept-Encoding: gzip%s", crlf); // ask for gzip only for full requests, because we intend to unzip instantly |
| 158 | cvecprintf(rawsnd, "%s", crlf); |
| 159 | |
| 160 | // send request |
| 161 | execcallback(0); |
| 162 | ENetBuffer buf; |
| 163 | buf.data = rawsnd.getbuf(); |
| 164 | buf.dataLength = rawsnd.length(); |
| 165 | while(buf.dataLength > 0) |
| 166 | { |
| 167 | enet_uint32 events = ENET_SOCKET_WAIT_SEND; |
| 168 | if(enet_socket_wait(tcp, &events, 250) >= 0 && events) |
| 169 | { |
| 170 | int sent = enet_socket_send(tcp, NULL, &buf, 1); |
| 171 | if(sent < 0) |
| 172 | { |
| 173 | if(!reconnected) |
| 174 | { // line went dead, maybe the server closed it - try reconnecting once |
| 175 | retry = true; |
| 176 | break; |
| 177 | } |
| 178 | else GETERROR("connection failed"); |
| 179 | } |
| 180 | buf.data = (char *)buf.data + sent; |
| 181 | buf.dataLength -= sent; |
| 182 | tcp_age.start(); |
| 183 | traffic += sent; |
| 184 | lastresponse = to.elapsed(); |
| 185 | } |
| 186 | elapsedtime = to.elapsed(); |
| 187 | if(elapsedtime > totaltimeout || elapsedtime > lastresponse + timeout) GETERROR("timeout 1"); |
| 188 | if(execcallback(-float(elapsedtime - lastresponse) / timeout)) goto geterror; // show progress bar, check for user interrupt |
| 189 | } |
| 190 | |
| 191 | // get response |
no test coverage detected