8 static const char darcs_version
[] = PACKAGE_VERSION
;
16 RESULT_REQUEST_NEW_FAIL
,
17 RESULT_NET_ADD_AFTER_FAIL
,
18 RESULT_REQUEST_ADD_CACHE_CONTROL_FAIL
,
19 RESULT_LOAD_TO_FILE_FAIL
,
22 RESULT_LIST_APPEND_OBJECT_FAIL
,
23 RESULT_EVENTLIST_NEW_LOOP
26 static const char *error_strings
[] =
29 "HTRequest_new() failed",
30 "HTNet_addAfter() failed",
31 "HTRequest_addCacheControl() failed",
32 "HTLoadToFile() failed",
34 "HTList_new() failed",
35 "HTList_appendObject() failed",
36 "HTEventList_newLoop() failed"
45 static BOOL debug
= NO
;
46 static BOOL init_done
= NO
;
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
,
58 struct Completed
*completed
= malloc(sizeof(struct Completed
));
59 if (completed
== NULL
)
60 error
= RESULT_MALLOC_FAIL
;
63 if (HTList_appendObject(completed_list
, completed
) == NO
)
65 error
= RESULT_LIST_APPEND_OBJECT_FAIL
;
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.
83 const char *libwww_request_url(const char *url
,
91 HTProfile_newNoCacheClient("darcs", darcs_version
);
93 HTAlert_setInteractive(NO
);
94 HTFormat_addCoding("*", NULL
, HTIdentityCoding
, 1.0);
96 HTSetTraceMessageMask("sop");
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();
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
);
115 return error_strings
[RESULT_NET_ADD_AFTER_FAIL
];
119 HTRequest_addGnHd(request
, HT_G_PRAGMA_NO_CACHE
);
120 result
= HTRequest_addCacheControl(request
, "no-cache", "");
122 else if (cache_time
> 0)
125 snprintf(buf
, sizeof(buf
), "%d", cache_time
);
126 buf
[sizeof(buf
) - 1] = '\0';
127 result
= HTRequest_addCacheControl(request
, "max-age", buf
);
130 return error_strings
[RESULT_REQUEST_ADD_CACHE_CONTROL_FAIL
];
132 result
= HTLoadToFile(url
, request
, filename
);
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
)
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';
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
);
171 return error_strings
[error
];
174 const char *libwww_last_url()
176 return last_url
!= NULL
? last_url
: "";
179 void libwww_enable_debug()