http.h 2.67 KB
Newer Older
1 2 3 4 5 6 7 8
#ifndef HTTP_H
#define HTTP_H

#include "cache.h"

#include <curl/curl.h>
#include <curl/easy.h>

Mike Hommey's avatar
Mike Hommey committed
9
#include "strbuf.h"
10
#include "remote.h"
Mike Hommey's avatar
Mike Hommey committed
11

12 13 14 15 16 17 18 19
/*
 * We detect based on the cURL version if multi-transfer is
 * usable in this implementation and define this symbol accordingly.
 * This is not something Makefile should set nor users should pass
 * via CFLAGS.
 */
#undef USE_CURL_MULTI

20
#if LIBCURL_VERSION_NUM >= 0x071000
21 22 23 24 25 26 27 28 29 30 31
#define USE_CURL_MULTI
#define DEFAULT_MAX_REQUESTS 5
#endif

#if LIBCURL_VERSION_NUM < 0x070704
#define curl_global_cleanup() do { /* nothing */ } while(0)
#endif
#if LIBCURL_VERSION_NUM < 0x070800
#define curl_global_init(a) do { /* nothing */ } while(0)
#endif

32
#if (LIBCURL_VERSION_NUM < 0x070c04) || (LIBCURL_VERSION_NUM == 0x071000)
33 34 35
#define NO_CURL_EASY_DUPHANDLE
#endif

36 37 38 39
#if LIBCURL_VERSION_NUM < 0x070a03
#define CURLE_HTTP_RETURNED_ERROR CURLE_HTTP_NOT_FOUND
#endif

40 41 42 43 44 45
struct slot_results
{
	CURLcode curl_result;
	long http_code;
};

46 47 48 49 50 51 52
struct active_request_slot
{
	CURL *curl;
	FILE *local;
	int in_use;
	CURLcode curl_result;
	long http_code;
Nick Hengeveld's avatar
Nick Hengeveld committed
53
	int *finished;
54
	struct slot_results *results;
55 56 57 58 59 60 61
	void *callback_data;
	void (*callback_func)(void *data);
	struct active_request_slot *next;
};

struct buffer
{
Mike Hommey's avatar
Mike Hommey committed
62 63
	struct strbuf buf;
	size_t posn;
64 65 66
};

/* Curl request read/write callbacks */
67 68 69
extern size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *strbuf);
extern size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf);
extern size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf);
70 71 72 73 74 75

/* Slot lifecycle functions */
extern struct active_request_slot *get_active_slot(void);
extern int start_active_slot(struct active_request_slot *slot);
extern void run_active_slot(struct active_request_slot *slot);
extern void finish_all_active_slots(void);
76
extern void release_active_slot(struct active_request_slot *slot);
77 78 79

#ifdef USE_CURL_MULTI
extern void fill_active_slots(void);
80
extern void add_fill_function(void *data, int (*fill)(void *));
81 82 83
extern void step_active_slots(void);
#endif

84
extern void http_init(struct remote *remote);
85 86 87 88 89 90 91
extern void http_cleanup(void);

extern int data_received;
extern int active_requests;

extern char curl_errorstr[CURL_ERROR_SIZE];

92 93 94 95 96 97 98 99 100 101 102 103 104
static inline int missing__target(int code, int result)
{
	return	/* file:// URL -- do we ever use one??? */
		(result == CURLE_FILE_COULDNT_READ_FILE) ||
		/* http:// and https:// URL */
		(code == 404 && result == CURLE_HTTP_RETURNED_ERROR) ||
		/* ftp:// URL */
		(code == 550 && result == CURLE_FTP_COULDNT_RETR_FILE)
		;
}

#define missing_target(a) missing__target((a)->http_code, (a)->curl_result)

105
extern int http_fetch_ref(const char *base, struct ref *ref);
106

107
#endif /* HTTP_H */