1 /* 2 * nghttp2 - HTTP/2 C Library 3 * 4 * Copyright (c) 2012 Tatsuhiro Tsujikawa 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining 7 * a copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sublicense, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 #ifndef NGHTTP2_GZIP_H 26 27 # ifdef HAVE_CONFIG_H 28 # include <config.h> 29 # endif /* HAVE_CONFIG_H */ 30 # include <zlib.h> 31 32 # include <nghttp2/nghttp2.h> 33 34 # ifdef __cplusplus 35 extern "C" { 36 # endif 37 38 /** 39 * @struct 40 * 41 * The gzip stream to inflate data. 42 */ 43 typedef struct { 44 z_stream zst; 45 int8_t finished; 46 } nghttp2_gzip; 47 48 /** 49 * @function 50 * 51 * A helper function to set up a per request gzip stream to inflate 52 * data. 53 * 54 * This function returns 0 if it succeeds, or -1. 55 */ 56 int nghttp2_gzip_inflate_new(nghttp2_gzip **inflater_ptr); 57 58 /** 59 * @function 60 * 61 * Frees the inflate stream. The |inflater| may be ``NULL``. 62 */ 63 void nghttp2_gzip_inflate_del(nghttp2_gzip *inflater); 64 65 /** 66 * @function 67 * 68 * Inflates data in |in| with the length |*inlen_ptr| and stores the 69 * inflated data to |out| which has allocated size at least 70 * |*outlen_ptr|. On return, |*outlen_ptr| is updated to represent 71 * the number of data written in |out|. Similarly, |*inlen_ptr| is 72 * updated to represent the number of input bytes processed. 73 * 74 * This function returns 0 if it succeeds, or -1. 75 * 76 * The example follows:: 77 * 78 * void on_data_chunk_recv_callback(nghttp2_session *session, 79 * uint8_t flags, 80 * int32_t stream_id, 81 * const uint8_t *data, size_t len, 82 * void *user_data) 83 * { 84 * ... 85 * req = nghttp2_session_get_stream_user_data(session, stream_id); 86 * nghttp2_gzip *inflater = req->inflater; 87 * while(len > 0) { 88 * uint8_t out[MAX_OUTLEN]; 89 * size_t outlen = MAX_OUTLEN; 90 * size_t tlen = len; 91 * int rv; 92 * rv = nghttp2_gzip_inflate(inflater, out, &outlen, data, &tlen); 93 * if(rv != 0) { 94 * nghttp2_submit_rst_stream(session, stream_id, 95 * NGHTTP2_INTERNAL_ERROR); 96 * break; 97 * } 98 * ... Do stuff ... 99 * data += tlen; 100 * len -= tlen; 101 * } 102 * .... 103 * } 104 */ 105 int nghttp2_gzip_inflate(nghttp2_gzip *inflater, uint8_t *out, 106 size_t *outlen_ptr, const uint8_t *in, 107 size_t *inlen_ptr); 108 109 /** 110 * @function 111 * 112 * Returns nonzero if |inflater| sees the end of deflate stream. 113 * After this function returns nonzero, `nghttp2_gzip_inflate()` with 114 * |inflater| gets to return error. 115 */ 116 int nghttp2_gzip_inflate_finished(nghttp2_gzip *inflater); 117 118 # ifdef __cplusplus 119 } 120 # endif 121 122 #endif /* NGHTTP2_GZIP_H */ 123