blob: aeba9301f8fe1a1d4e2f9819257579375be648aa [file] [log] [blame]
Nick Hengeveld29508e12005-11-18 11:02:58 -08001#ifndef HTTP_H
2#define HTTP_H
3
4#include "cache.h"
5
6#include <curl/curl.h>
7#include <curl/easy.h>
8
Mike Hommey028c2972007-12-09 20:30:59 +01009#include "strbuf.h"
10
Alexandre Julliard9cf04302007-05-02 14:53:23 +020011#if LIBCURL_VERSION_NUM >= 0x071000
Nick Hengeveld29508e12005-11-18 11:02:58 -080012#define USE_CURL_MULTI
13#define DEFAULT_MAX_REQUESTS 5
14#endif
15
16#if LIBCURL_VERSION_NUM < 0x070704
17#define curl_global_cleanup() do { /* nothing */ } while(0)
18#endif
19#if LIBCURL_VERSION_NUM < 0x070800
20#define curl_global_init(a) do { /* nothing */ } while(0)
21#endif
22
Junio C Hamano500ebb02006-12-27 13:59:26 -080023#if (LIBCURL_VERSION_NUM < 0x070c04) || (LIBCURL_VERSION_NUM == 0x071000)
Nick Hengeveld29508e12005-11-18 11:02:58 -080024#define NO_CURL_EASY_DUPHANDLE
25#endif
26
Art Haasc774b2d2006-09-19 07:20:19 -050027#if LIBCURL_VERSION_NUM < 0x070a03
28#define CURLE_HTTP_RETURNED_ERROR CURLE_HTTP_NOT_FOUND
29#endif
30
Nick Hengeveldc8568e12006-01-31 11:06:55 -080031struct slot_results
32{
33 CURLcode curl_result;
34 long http_code;
35};
36
Nick Hengeveld29508e12005-11-18 11:02:58 -080037struct active_request_slot
38{
39 CURL *curl;
40 FILE *local;
41 int in_use;
42 CURLcode curl_result;
43 long http_code;
Nick Hengeveldbaa7b672006-03-10 20:18:01 -080044 int *finished;
Nick Hengeveldc8568e12006-01-31 11:06:55 -080045 struct slot_results *results;
Nick Hengeveld29508e12005-11-18 11:02:58 -080046 void *callback_data;
47 void (*callback_func)(void *data);
48 struct active_request_slot *next;
49};
50
51struct buffer
52{
Mike Hommey028c2972007-12-09 20:30:59 +010053 struct strbuf buf;
54 size_t posn;
Nick Hengeveld29508e12005-11-18 11:02:58 -080055};
56
57/* Curl request read/write callbacks */
58extern size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb,
59 struct buffer *buffer);
60extern size_t fwrite_buffer(const void *ptr, size_t eltsize,
Mike Hommey028c2972007-12-09 20:30:59 +010061 size_t nmemb, struct strbuf *buffer);
Nick Hengeveld29508e12005-11-18 11:02:58 -080062extern size_t fwrite_null(const void *ptr, size_t eltsize,
Mike Hommey028c2972007-12-09 20:30:59 +010063 size_t nmemb, struct strbuf *buffer);
Nick Hengeveld29508e12005-11-18 11:02:58 -080064
65/* Slot lifecycle functions */
66extern struct active_request_slot *get_active_slot(void);
67extern int start_active_slot(struct active_request_slot *slot);
68extern void run_active_slot(struct active_request_slot *slot);
69extern void finish_all_active_slots(void);
Mark Wooding53f31382006-02-07 10:07:39 +000070extern void release_active_slot(struct active_request_slot *slot);
Nick Hengeveld29508e12005-11-18 11:02:58 -080071
72#ifdef USE_CURL_MULTI
73extern void fill_active_slots(void);
Daniel Barkalowfc57b6a2007-09-10 23:02:34 -040074extern void add_fill_function(void *data, int (*fill)(void *));
Nick Hengeveld29508e12005-11-18 11:02:58 -080075extern void step_active_slots(void);
76#endif
77
78extern void http_init(void);
79extern void http_cleanup(void);
80
81extern int data_received;
82extern int active_requests;
83
Nick Hengeveld29508e12005-11-18 11:02:58 -080084extern char curl_errorstr[CURL_ERROR_SIZE];
85
Mike Hommeye8dc37e2007-12-10 22:36:09 +010086static inline int missing__target(int code, int result)
87{
88 return /* file:// URL -- do we ever use one??? */
89 (result == CURLE_FILE_COULDNT_READ_FILE) ||
90 /* http:// and https:// URL */
91 (code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
92 /* ftp:// URL */
93 (code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
94 ;
95}
96
97#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
98
Mike Hommeyd7e92802007-12-11 00:08:25 +010099extern int http_fetch_ref(const char *base, const char *ref, unsigned char *sha1);
100
Nick Hengeveld29508e12005-11-18 11:02:58 -0800101#endif /* HTTP_H */