mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
store_ro_http_proxy.c File Reference
#include <glib.h>
#include <stdio.h>
#include "config.h"
#include "g_logger.h"
#include "store.h"
#include "store_ro_http_proxy.h"
#include <curl/curl.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

Functions

static size_t write_memory_callback (void *contents, size_t size, size_t nmemb, void *userp)
 libcurl CURLOPT_WRITEFUNCTION callback appending response bytes into a MemoryStruct.
static char * ro_http_proxy_xyz_to_storagekey (struct storage_backend *store, int x, int y, int z, char *key)
 Format the upstream HTTP URL for tile (x, y, z).
static int ro_http_proxy_tile_retrieve (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z)
 Fetch a tile over HTTP from the upstream server or serve it from the single-tile cache.
static int ro_http_proxy_tile_read (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, char *buf, size_t sz, int *compressed, char *log_msg)
 Retrieve a PNG tile from the upstream HTTP proxy into caller buffer buf.
static struct stat_info ro_http_proxy_tile_stat (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z)
 Retrieve metadata and Last-Modified timestamp for a tile from the HTTP proxy.
static char * ro_http_proxy_tile_storage_id (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, char *string)
 Format the upstream "http://<baseurl>/<z>/<x>/<y>.png" URL as the tile storage ID.
static int ro_http_proxy_metatile_write (struct storage_backend *store, const char *xmlconfig, const char *options, int x, int y, int z, const char *buf, int sz)
 Reject metatile write attempts on the read-only HTTP proxy backend.
static int ro_http_proxy_metatile_delete (struct storage_backend *store, const char *xmlconfig, int x, int y, int z)
 Reject metatile delete attempts on the read-only HTTP proxy backend.
static int ro_http_proxy_metatile_expire (struct storage_backend *store, const char *xmlconfig, int x, int y, int z)
 Reject metatile expiry attempts on the read-only HTTP proxy backend.
static int ro_http_proxy_close_storage (struct storage_backend *store)
 Free the base URL, cached tile buffer, CURL easy handle, and ro_http_proxy_ctx.
struct storage_backend * init_storage_ro_http_proxy (const char *connection_string)
 Allocate and initialize a read-only HTTP proxy storage_backend.

Variables

static pthread_mutex_t ro_http_proxy_mutex
 Mutex serializing one-time curl_global_init() across worker threads.
static int done_global_init = 0
 Process-wide flag indicating whether curl_global_init() has already run.

Function Documentation

◆ init_storage_ro_http_proxy()

struct storage_backend * init_storage_ro_http_proxy ( const char * connection_string)

Allocate and initialize a read-only HTTP proxy storage_backend.

Strips the "ro_http_proxy://" prefix from connection_string to obtain the upstream base host/path, performs thread-safe one-time curl_global_init() under ro_http_proxy_mutex, configures a CURL easy handle with CURLOPT_NOSIGNAL, CURLOPT_FOLLOWLOCATION, CURLOPT_USERAGENT, and CURLOPT_FILETIME, and wires all backend operation function pointers. Returns NULL if compiled without HAVE_LIBCURL or if initialization fails.

Parameters
connection_stringBackend URI of the form "ro_http_proxy://<host[:port][/prefix]>".
Returns
Newly allocated struct storage_backend pointer, or NULL on failure or if unsupported.

◆ ro_http_proxy_close_storage()

int ro_http_proxy_close_storage ( struct storage_backend * store)
static

Free the base URL, cached tile buffer, CURL easy handle, and ro_http_proxy_ctx.

Parameters
storeHTTP proxy storage_backend instance to close.
Returns
Always returns 0.

◆ ro_http_proxy_metatile_delete()

int ro_http_proxy_metatile_delete ( struct storage_backend * store,
const char * xmlconfig,
int x,
int y,
int z )
static

Reject metatile delete attempts on the read-only HTTP proxy backend.

Parameters
storeHTTP proxy storage_backend (unused).
xmlconfigLogical map style name (unused).
xTile X coordinate (unused).
yTile Y coordinate (unused).
zTile zoom level (unused).
Returns
Always returns -1.

◆ ro_http_proxy_metatile_expire()

int ro_http_proxy_metatile_expire ( struct storage_backend * store,
const char * xmlconfig,
int x,
int y,
int z )
static

Reject metatile expiry attempts on the read-only HTTP proxy backend.

Parameters
storeHTTP proxy storage_backend (unused).
xmlconfigLogical map style name (unused).
xTile X coordinate (unused).
yTile Y coordinate (unused).
zTile zoom level (unused).
Returns
Always returns -1.

◆ ro_http_proxy_metatile_write()

int ro_http_proxy_metatile_write ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
const char * buf,
int sz )
static

Reject metatile write attempts on the read-only HTTP proxy backend.

Parameters
storeHTTP proxy storage_backend (unused).
xmlconfigLogical map style name (unused).
optionsStyle parameterization options string (unused).
xTile X coordinate (unused).
yTile Y coordinate (unused).
zTile zoom level (unused).
bufSerialized metatile buffer (unused).
szByte length of buf (unused).
Returns
Always returns -1.

◆ ro_http_proxy_tile_read()

int ro_http_proxy_tile_read ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
char * buf,
size_t sz,
int * compressed,
char * log_msg )
static

Retrieve a PNG tile from the upstream HTTP proxy into caller buffer buf.

Ensures the tile is loaded into ctx->cache via ro_http_proxy_tile_retrieve() and copies the cached bytes into buf if present and within capacity sz.

Parameters
storeInitialized HTTP proxy storage_backend.
xmlconfigLogical map style name.
optionsStyle parameterization options string.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
bufCaller-supplied output buffer receiving the tile PNG bytes.
szMaximum capacity of buf in bytes.
compressedCompression flag output pointer (unused; raw PNG is returned).
log_msgCaller-supplied error message buffer (unused; errors go to g_logger).
Returns
Number of bytes copied into buf (>= 0) on success, or -1 on 404, buffer overrun, or HTTP error.

◆ ro_http_proxy_tile_retrieve()

int ro_http_proxy_tile_retrieve ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z )
static

Fetch a tile over HTTP from the upstream server or serve it from the single-tile cache.

Checks whether (xmlconfig, x, y, z) matches ctx->cache. On a cache hit, returns 1 immediately without performing network I/O. On a cache miss, issues an HTTP GET via curl_easy_perform() and updates ctx->cache:

  • HTTP 200: Stores the downloaded PNG bytes, byte size, and Last-Modified timestamp (st_mtime).
  • HTTP 404: Caches a negative lookup (st_stat.size = -1) so the subsequent tile_read() avoids a duplicate request.
  • Other status codes or curl errors: Invalidates ctx->cache coordinates and returns -1.
Parameters
storeInitialized HTTP proxy storage_backend.
xmlconfigLogical map style name used as part of the single-tile cache key.
optionsStyle parameterization options string (currently unused).
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
Returns
1 if the tile result (HTTP 200 or cached 404) is ready in ctx->cache, or -1 on network/HTTP error.

◆ ro_http_proxy_tile_stat()

struct stat_info ro_http_proxy_tile_stat ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z )
static

Retrieve metadata and Last-Modified timestamp for a tile from the HTTP proxy.

Fetches (or hits cache for) the tile via ro_http_proxy_tile_retrieve() and returns ctx->cache.st_stat (size = -1 on HTTP 404 or fetch error).

Parameters
storeInitialized HTTP proxy storage_backend.
xmlconfigLogical map style name.
optionsStyle parameterization options string.
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
Returns
Populated struct stat_info (size < 0 indicates missing tile or HTTP error).

◆ ro_http_proxy_tile_storage_id()

char * ro_http_proxy_tile_storage_id ( struct storage_backend * store,
const char * xmlconfig,
const char * options,
int x,
int y,
int z,
char * string )
static

Format the upstream "http://<baseurl>/<z>/<x>/<y>.png" URL as the tile storage ID.

Parameters
storeInitialized HTTP proxy storage_backend.
xmlconfigLogical map style name (unused).
optionsStyle parameterization options string (unused).
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
stringCaller-supplied output buffer (PATH_MAX bytes) receiving the formatted URL.
Returns
Pointer to string.

◆ ro_http_proxy_xyz_to_storagekey()

char * ro_http_proxy_xyz_to_storagekey ( struct storage_backend * store,
int x,
int y,
int z,
char * key )
static

Format the upstream HTTP URL for tile (x, y, z).

Formats key as "http://<baseurl>/<z>/<x>/<y>.png".

Parameters
storeInitialized HTTP proxy storage_backend (storage_ctx holds struct ro_http_proxy_ctx *).
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
keyCaller-supplied output buffer (PATH_MAX bytes) receiving the formatted URL.
Returns
Pointer to key.

◆ write_memory_callback()

size_t write_memory_callback ( void * contents,
size_t size,
size_t nmemb,
void * userp )
static

libcurl CURLOPT_WRITEFUNCTION callback appending response bytes into a MemoryStruct.

Dynamically grows chunk->memory via realloc() / malloc() as HTTP response chunks arrive.

Parameters
contentsPointer to the incoming data chunk delivered by libcurl.
sizeSize of each element in bytes.
nmembNumber of elements in contents.
userpPointer to a struct MemoryStruct accumulating the response body.
Returns
Total number of bytes handled (size * nmemb).

Variable Documentation

◆ done_global_init

int done_global_init = 0
static

Process-wide flag indicating whether curl_global_init() has already run.

◆ ro_http_proxy_mutex

pthread_mutex_t ro_http_proxy_mutex
static

Mutex serializing one-time curl_global_init() across worker threads.