summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/HttpClient.cc64
1 files changed, 49 insertions, 15 deletions
diff --git a/lib/HttpClient.cc b/lib/HttpClient.cc
index dedd23a..cc636ed 100644
--- a/lib/HttpClient.cc
+++ b/lib/HttpClient.cc
@@ -1,3 +1,4 @@
+#include <TaskMan.h>
#include <HttpClient.h>
#include <BRegex.h>
#include <CopyJob.h>
@@ -5,6 +6,8 @@
t_headers no_headers;
+const timeval timeout = { 8, 0 };
+
HttpClient::HttpClient(const String & _url, Handle * _out, const String & _fake_host, t_headers _headers) : url(_url), out(_out), fake_host(_fake_host), headers(_headers), host(""), uri("") {
DecodeURL();
@@ -14,6 +17,7 @@ HttpClient::HttpClient(const String & _url, Handle * _out, const String & _fake_
if (Client.IsConnected()) {
SetBurst();
} else {
+ WaitFor(timeout);
WaitFor(&Client, W4_WRITING);
}
}
@@ -30,15 +34,18 @@ int HttpClient::Do() throw (GeneralException) {
switch (current) {
case 0:
- if (Client.IsConnecting()) {
+ if (Client.IsConnecting() && (TaskMan::Event() == Task::EVT_HANDLE)) {
Client.FinalizeConnect();
}
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ return TASK_DONE;
+ }
if (!Client.IsConnected()) {
return TASK_DONE;
}
- current = 1;
+ RemoveTimeout(); WaitFor(timeout);
- case 1:
b << "GET " + uri + " HTTP/1.1\r\n"
"Host: " + host + "\r\n"
"Connection: close\r\n";
@@ -51,20 +58,32 @@ int HttpClient::Do() throw (GeneralException) {
c = new CopyJob(&b, &Client);
WaitFor(c);
- current = 2;
+ current = 1;
Suspend(TASK_ON_HOLD);
- case 2:
+ case 1:
delete c;
-
+
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ return TASK_DONE;
+ }
+ RemoveTimeout(); WaitFor(timeout);
+
c = new ReadJob(&Client, &b);
WaitFor(c);
- current = 3;
+ current = 2;
Suspend(TASK_ON_HOLD);
- case 3:
+ case 2:
delete c;
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ return TASK_DONE;
+ }
+ RemoveTimeout(); WaitFor(timeout);
+
b >> t;
if (!h_reply.Match(t)) {
http_code = 0;
@@ -92,7 +111,7 @@ int HttpClient::Do() throw (GeneralException) {
status = "Downloading.";
if (!chunked) {
- current = 6;
+ current = 5;
c = new CopyJob(&Client, out);
WaitFor(c);
Suspend(TASK_ON_HOLD);
@@ -100,19 +119,34 @@ int HttpClient::Do() throw (GeneralException) {
c = 0;
- case 4:
+ case 3:
if (c)
delete c;
+
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ return TASK_DONE;
+ }
+ RemoveTimeout(); WaitFor(timeout);
+
c = new ReadJob(&Client, &b, any);
WaitFor(c);
- current = 5;
+ current = 4;
Suspend(TASK_ON_HOLD);
- case 5:
+ case 4:
delete c;
+
+ if (TaskMan::Event() == Task::EVT_TIMEOUT) {
+ status = "Connection timeout.";
+ return TASK_DONE;
+ }
+ RemoveTimeout(); WaitFor(timeout);
+
+
b >> t;
if (t.strlen() == 0) {
- current = 4;
+ current = 3;
c = 0;
Suspend(TASK_BURST);
}
@@ -125,10 +159,10 @@ int HttpClient::Do() throw (GeneralException) {
c = new CopyJob(&Client, out, l);
WaitFor(c);
- current = 4;
+ current = 3;
Suspend(TASK_ON_HOLD);
- case 6:
+ case 5:
delete c;
status = "Downloaded.";