Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00043 #include "parse_expires.h"
00044 #include <stdio.h>
00045 #include "../mem/mem.h"
00046 #include "../dprint.h"
00047 #include "../trim.h"
00048 #include <string.h>
00049 #include "../ut.h"
00050
00051
00052 static inline int expires_parser(char* _s, int _l, exp_body_t* _e)
00053 {
00054 int i;
00055 str tmp;
00056
00057 tmp.s = _s;
00058 tmp.len = _l;
00059
00060 trim(&tmp);
00061
00062 if (tmp.len == 0) {
00063 LOG(L_ERR, "expires_parser(): Empty body\n");
00064 _e->valid = 0;
00065 return -1;
00066 }
00067
00068 _e->text.s = tmp.s;
00069 _e->text.len = tmp.len;
00070
00071
00072 if (tmp.len > 10) {
00073 _e->valid = 0;
00074 return 0;
00075 }
00076
00077 for(i = 0; i < tmp.len; i++) {
00078 if ((tmp.s[i] >= '0') && (tmp.s[i] <= '9')) {
00079 _e->val *= 10;
00080 _e->val += tmp.s[i] - '0';
00081 } else {
00082 switch(tmp.s[i]) {
00083 case ' ':
00084 case '\t':
00085 case '\r':
00086 case '\n':
00087 _e->text.len = i;
00088 _e->valid = 1;
00089 return 0;
00090
00091 default:
00092
00093
00094
00095
00096
00097
00098
00099 _e->valid = 0;
00100 return 0;
00101 }
00102 }
00103 }
00104
00105 _e->valid = 1;
00106 return 0;
00107 }
00108
00109
00113 int parse_expires(struct hdr_field* _h)
00114 {
00115 exp_body_t* e;
00116
00117 if (_h->parsed) {
00118 return 0;
00119 }
00120
00121 e = (exp_body_t*)pkg_malloc(sizeof(exp_body_t));
00122 if (e == 0) {
00123 LOG(L_ERR, "parse_expires(): No memory left\n");
00124 return -1;
00125 }
00126
00127 memset(e, 0, sizeof(exp_body_t));
00128
00129 if (expires_parser(_h->body.s, _h->body.len, e) < 0) {
00130 LOG(L_ERR, "parse_expires(): Error while parsing\n");
00131 pkg_free(e);
00132 return -2;
00133 }
00134
00135 _h->parsed = (void*)e;
00136 return 0;
00137 }
00138
00139
00143 void free_expires(exp_body_t** _e)
00144 {
00145 pkg_free(*_e);
00146 *_e = 0;
00147 }
00148
00149
00153 void print_expires(exp_body_t* _e)
00154 {
00155 printf("===Expires===\n");
00156 printf("text: \'%.*s\'\n", _e->text.len, ZSW(_e->text.s));
00157 printf("val : %d\n", _e->val);
00158 printf("===/Expires===\n");
00159 }