00001 /* 00002 * Copyright 2008-2010 Arsen Chaloyan 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 * 00016 * $Id: apt_header_field.h 1719 2010-05-31 21:09:51Z achaloyan $ 00017 */ 00018 00019 #ifndef APT_HEADER_FIELD_H 00020 #define APT_HEADER_FIELD_H 00021 00022 /** 00023 * @file apt_header_field.h 00024 * @brief Header Field Declaration (RFC5322) 00025 */ 00026 00027 #ifdef WIN32 00028 #pragma warning(disable: 4127) 00029 #endif 00030 #include <apr_ring.h> 00031 #include "apt_string.h" 00032 00033 APT_BEGIN_EXTERN_C 00034 00035 /** Header field declaration */ 00036 typedef struct apt_header_field_t apt_header_field_t; 00037 /** Header section declaration */ 00038 typedef struct apt_header_section_t apt_header_section_t; 00039 00040 /** Header field */ 00041 struct apt_header_field_t { 00042 /** Ring entry */ 00043 APR_RING_ENTRY(apt_header_field_t) link; 00044 00045 /** Name of the header field */ 00046 apt_str_t name; 00047 /** Value of the header field */ 00048 apt_str_t value; 00049 00050 /** Numeric identifier associated with name */ 00051 apr_size_t id; 00052 }; 00053 00054 /** 00055 * Header section 00056 * @remark The header section is a collection of header fields. 00057 * The header fields are stored in both a ring and an array. 00058 * The goal is to ensure efficient access and manipulation on the header fields. 00059 */ 00060 struct apt_header_section_t { 00061 /** List of header fields (name-value pairs) */ 00062 APR_RING_HEAD(apt_head_t, apt_header_field_t) ring; 00063 /** Array of pointers to header fields */ 00064 apt_header_field_t **arr; 00065 /** Max number of header fields */ 00066 apr_size_t arr_size; 00067 }; 00068 00069 00070 /** 00071 * Allocate an empty header field. 00072 * @param pool the pool to allocate memory from 00073 */ 00074 APT_DECLARE(apt_header_field_t*) apt_header_field_alloc(apr_pool_t *pool); 00075 00076 /** 00077 * Create a header field using given name and value APT strings. 00078 * @param name the name of the header field 00079 * @param value the value of the header field 00080 * @param pool the pool to allocate memory from 00081 */ 00082 APT_DECLARE(apt_header_field_t*) apt_header_field_create(const apt_str_t *name, const apt_str_t *value, apr_pool_t *pool); 00083 00084 /** 00085 * Create a header field using given name and value C strings. 00086 * @param name the name of the header field 00087 * @param value the value of the header field 00088 * @param pool the pool to allocate memory from 00089 */ 00090 APT_DECLARE(apt_header_field_t*) apt_header_field_create_c(const char *name, const char *value, apr_pool_t *pool); 00091 00092 /** 00093 * Create a header field from entire text line consisting of a name and value pair. 00094 * @param line the text line, which consists of a name and value pair 00095 * @param separator the name and value separator 00096 * @param pool the pool to allocate memory from 00097 */ 00098 APT_DECLARE(apt_header_field_t*) apt_header_field_create_from_line(const apt_str_t *line, char separator, apr_pool_t *pool); 00099 00100 /** 00101 * Copy specified header field. 00102 * @param src_header_field the header field to copy 00103 * @param pool the pool to allocate memory from 00104 */ 00105 APT_DECLARE(apt_header_field_t*) apt_header_field_copy(const apt_header_field_t *src_header_field, apr_pool_t *pool); 00106 00107 /** 00108 * Initialize header section (collection of header fields). 00109 * @param header the header section to initialize 00110 */ 00111 APT_DECLARE(void) apt_header_section_init(apt_header_section_t *header); 00112 00113 /** 00114 * Allocate header section to set/get header fields by numeric identifiers. 00115 * @param header the header section to allocate 00116 * @param max_field_count the max number of header fields in the section (protocol dependent) 00117 * @param pool the pool to allocate memory from 00118 */ 00119 APT_DECLARE(apt_bool_t) apt_header_section_array_alloc(apt_header_section_t *header, apr_size_t max_field_count, apr_pool_t *pool); 00120 00121 /** 00122 * Add (append) header field to header section. 00123 * @param header the header section to add field to 00124 * @param header_field the header field to add 00125 */ 00126 APT_DECLARE(apt_bool_t) apt_header_section_field_add(apt_header_section_t *header, apt_header_field_t *header_field); 00127 00128 /** 00129 * Insert header field to header section based on numreic identifier if specified. 00130 * @param header the header section to insert field into 00131 * @param header_field the header field to insert 00132 */ 00133 APT_DECLARE(apt_bool_t) apt_header_section_field_insert(apt_header_section_t *header, apt_header_field_t *header_field); 00134 00135 /** 00136 * Set header field in the array of header fields using associated numeric identifier. 00137 * @param header the header section to set field for 00138 * @param header_field the header field to set 00139 * @remark Typically, the header field should be already added to the header section using apt_header_section_field_add() 00140 */ 00141 APT_DECLARE(apt_bool_t) apt_header_section_field_set(apt_header_section_t *header, apt_header_field_t *header_field); 00142 00143 /** 00144 * Remove header field from header section. 00145 * @param header the header section to remove field from 00146 * @param header_field the header field to remove 00147 */ 00148 APT_DECLARE(apt_bool_t) apt_header_section_field_remove(apt_header_section_t *header, apt_header_field_t *header_field); 00149 00150 /** 00151 * Check whether specified header field is set. 00152 * @param header the header section to use 00153 * @param id the identifier associated with the header_field to check 00154 */ 00155 static APR_INLINE apt_bool_t apt_header_section_field_check(const apt_header_section_t *header, apr_size_t id) 00156 { 00157 if(id < header->arr_size) { 00158 return header->arr[id] ? TRUE : FALSE; 00159 } 00160 return FALSE; 00161 } 00162 00163 /** 00164 * Get header field by specified identifier. 00165 * @param header the header section to use 00166 * @param id the identifier associated with the header_field 00167 */ 00168 static APR_INLINE apt_header_field_t* apt_header_section_field_get(const apt_header_section_t *header, apr_size_t id) 00169 { 00170 if(id < header->arr_size) { 00171 return header->arr[id]; 00172 } 00173 return NULL; 00174 } 00175 00176 APT_END_EXTERN_C 00177 00178 #endif /* APT_HEADER_FIELD_H */