Follow upstream changes -- Bytestring updates
[git-darcs-import.git] / src / hslibwww.c
blob163ca0309f46958bdca7c4410d44c1ef24ddd1e8
1 #ifdef HAVE_LIBWWW
3 #include "hslibwww.h"
5 #include <stdio.h>
6 #include <string.h>
8 static const char darcs_version[] = PACKAGE_VERSION;
10 #include <WWWLib.h>
11 #include <WWWInit.h>
13 enum RESULT_CODES
15 RESULT_OK = 0,
16 RESULT_REQUEST_NEW_FAIL,
17 RESULT_NET_ADD_AFTER_FAIL,
18 RESULT_REQUEST_ADD_CACHE_CONTROL_FAIL,
19 RESULT_LOAD_TO_FILE_FAIL,
20 RESULT_MALLOC_FAIL,
21 RESULT_LIST_NEW_FAIL,
22 RESULT_LIST_APPEND_OBJECT_FAIL,
23 RESULT_EVENTLIST_NEW_LOOP
26 static const char *error_strings[] =
28 "",
29 "HTRequest_new() failed",
30 "HTNet_addAfter() failed",
31 "HTRequest_addCacheControl() failed",
32 "HTLoadToFile() failed",
33 "malloc() failed",
34 "HTList_new() failed",
35 "HTList_appendObject() failed",
36 "HTEventList_newLoop() failed"
39 struct Completed
41 int error;
42 char *url;
45 static BOOL debug = NO;
46 static BOOL init_done = NO;
47 static int error;
48 static HTList *completed_list = NULL;
49 static char *last_url = NULL;
50 static char libwww_error[80];
51 static const char libwww_error_fmt[] = "libwww error code: %i";
53 int terminate_handler(HTRequest *request,
54 HTResponse *response,
55 void *param,
56 int status)
58 struct Completed *completed = malloc(sizeof(struct Completed));
59 if (completed == NULL)
60 error = RESULT_MALLOC_FAIL;
61 else
63 if (HTList_appendObject(completed_list, completed) == NO)
65 error = RESULT_LIST_APPEND_OBJECT_FAIL;
66 free(completed);
68 else
70 error = RESULT_OK;
71 completed->error = status;
72 completed->url = HTRequest_context(request);
76 HTRequest_delete(request);
77 HTEventList_stopLoop();
79 // Return not HT_OK to stop executing after filters.
80 return HT_ERROR;
83 const char *libwww_request_url(const char *url,
84 const char *filename,
85 int cache_time)
87 BOOL result;
89 if (init_done == NO)
91 HTProfile_newNoCacheClient("darcs", darcs_version);
92 HTProxy_getEnvVar();
93 HTAlert_setInteractive(NO);
94 HTFormat_addCoding("*", NULL, HTIdentityCoding, 1.0);
95 if (debug == YES)
96 HTSetTraceMessageMask("sop");
97 init_done = YES;
100 if (completed_list == NULL)
102 completed_list = HTList_new();
103 if (completed_list == NULL)
104 return error_strings[RESULT_LIST_NEW_FAIL];
107 HTRequest *const request = HTRequest_new();
108 if (request == NULL)
109 return error_strings[RESULT_REQUEST_NEW_FAIL];
111 HTRequest_setContext(request, strdup(url));
113 result = HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
114 if (result == NO)
115 return error_strings[RESULT_NET_ADD_AFTER_FAIL];
117 if (cache_time == 0)
119 HTRequest_addGnHd(request, HT_G_PRAGMA_NO_CACHE);
120 result = HTRequest_addCacheControl(request, "no-cache", "");
122 else if (cache_time > 0)
124 char buf[8];
125 snprintf(buf, sizeof(buf), "%d", cache_time);
126 buf[sizeof(buf) - 1] = '\0';
127 result = HTRequest_addCacheControl(request, "max-age", buf);
129 if (result == NO)
130 return error_strings[RESULT_REQUEST_ADD_CACHE_CONTROL_FAIL];
132 result = HTLoadToFile(url, request, filename);
133 if (result == NO)
134 return error_strings[RESULT_LOAD_TO_FILE_FAIL];
136 return error_strings[RESULT_OK];
139 const char *libwww_wait_next_url()
141 if (last_url != NULL)
143 free(last_url);
144 last_url = NULL;
147 error = RESULT_OK;
148 if (HTList_isEmpty(completed_list) == YES &&
149 HTNet_isEmpty() == NO &&
150 HTEventList_newLoop() != HT_OK)
151 return error_strings[RESULT_EVENTLIST_NEW_LOOP];
153 if (HTList_isEmpty(completed_list) == NO)
155 struct Completed *completed = HTList_firstObject(completed_list);
156 if (completed->error == HT_LOADED)
157 libwww_error[0] = '\0';
158 else
160 snprintf(libwww_error, sizeof(libwww_error),
161 libwww_error_fmt, completed->error);
162 libwww_error[sizeof(libwww_error) - 1] = '\0';
164 last_url = completed->url;
165 HTList_removeFirstObject(completed_list);
166 free(completed);
168 return libwww_error;
171 return error_strings[error];
174 const char *libwww_last_url()
176 return last_url != NULL ? last_url : "";
179 void libwww_enable_debug()
181 debug = YES;
184 #endif