* if curl sees a 3xx code, a Location header and a Connection:close header it decides...
[m6w6/ext-http] / php_http_etag.c
1 #include "php_http.h"
2
3 #ifdef PHP_HTTP_HAVE_HASH
4 # include "php_hash.h"
5 #endif
6
7 #include <ext/standard/crc32.h>
8 #include <ext/standard/sha1.h>
9 #include <ext/standard/md5.h>
10
11 PHP_HTTP_API void *php_http_etag_init(TSRMLS_D)
12 {
13 void *ctx = NULL;
14 char *mode = PHP_HTTP_G->env.etag_mode;
15
16 #ifdef PHP_HTTP_HAVE_HASH
17 const php_hash_ops *eho = NULL;
18
19 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
20 ctx = emalloc(eho->context_size);
21 eho->hash_init(ctx);
22 } else
23 #endif
24 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
25 ctx = emalloc(sizeof(uint));
26 *((uint *) ctx) = ~0;
27 } else if (mode && !strcasecmp(mode, "sha1")) {
28 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
29 } else {
30 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
31 }
32
33 return ctx;
34 }
35
36 PHP_HTTP_API char *php_http_etag_finish(void *ctx TSRMLS_DC)
37 {
38 unsigned char digest[128] = {0};
39 char *etag = NULL, *mode = PHP_HTTP_G->env.etag_mode;
40
41 #ifdef PHP_HTTP_HAVE_HASH
42 const php_hash_ops *eho = NULL;
43
44 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
45 eho->hash_final(digest, ctx);
46 etag = php_http_etag_digest(digest, eho->digest_size);
47 } else
48 #endif
49 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
50 *((uint *) ctx) = ~*((uint *) ctx);
51 etag = php_http_etag_digest((const unsigned char *) ctx, sizeof(uint));
52 } else if (mode && (!strcasecmp(mode, "sha1"))) {
53 PHP_SHA1Final(digest, ctx);
54 etag = php_http_etag_digest(digest, 20);
55 } else {
56 PHP_MD5Final(digest, ctx);
57 etag = php_http_etag_digest(digest, 16);
58 }
59 efree(ctx);
60
61 return etag;
62 }
63
64 PHP_HTTP_API size_t php_http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC)
65 {
66 char *mode = PHP_HTTP_G->env.etag_mode;
67 #ifdef PHP_HTTP_HAVE_HASH
68 const php_hash_ops *eho = NULL;
69
70 if (mode && (eho = php_hash_fetch_ops(mode, strlen(mode)))) {
71 eho->hash_update(ctx, (const unsigned char *) data_ptr, data_len);
72 } else
73 #endif
74 if (mode && ((!strcasecmp(mode, "crc32")) || (!strcasecmp(mode, "crc32b")))) {
75 uint i, c = *((uint *) ctx);
76 for (i = 0; i < data_len; ++i) {
77 CRC32(c, data_ptr[i]);
78 }
79 *((uint *)ctx) = c;
80 } else if (mode && (!strcasecmp(mode, "sha1"))) {
81 PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
82 } else {
83 PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
84 }
85
86 return data_len;
87 }
88