summaryrefslogtreecommitdiffabout
path: root/parsing.c
authorLars Hjemli <hjemli@gmail.com>2008-04-08 19:11:36 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-04-08 19:11:36 (UTC)
commite87e89633383b8b75c68c98be3e0c14212109de2 (patch) (unidiff)
treef57e131ab854b58023387aee8efc0e4ee54653b5 /parsing.c
parent20a33548b9a87a6eb23162ee5d137daa46d78613 (diff)
downloadcgit-e87e89633383b8b75c68c98be3e0c14212109de2.zip
cgit-e87e89633383b8b75c68c98be3e0c14212109de2.tar.gz
cgit-e87e89633383b8b75c68c98be3e0c14212109de2.tar.bz2
Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
This is a generic http-function. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (limited to 'parsing.c') (more/less context) (ignore whitespace changes)
-rw-r--r--parsing.c49
1 files changed, 0 insertions, 49 deletions
diff --git a/parsing.c b/parsing.c
index 9a4a7a3..66e8b3d 100644
--- a/parsing.c
+++ b/parsing.c
@@ -1,257 +1,208 @@
1/* config.c: parsing of config files 1/* config.c: parsing of config files
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11char *convert_query_hexchar(char *txt)
12{
13 int d1, d2;
14 if (strlen(txt) < 3) {
15 *txt = '\0';
16 return txt-1;
17 }
18 d1 = hextoint(*(txt+1));
19 d2 = hextoint(*(txt+2));
20 if (d1<0 || d2<0) {
21 strcpy(txt, txt+3);
22 return txt-1;
23 } else {
24 *txt = d1 * 16 + d2;
25 strcpy(txt+1, txt+3);
26 return txt;
27 }
28}
29
30int cgit_parse_query(char *txt, configfn fn)
31{
32 char *t, *value = NULL, c;
33
34 if (!txt)
35 return 0;
36
37 t = txt = xstrdup(txt);
38
39 while((c=*t) != '\0') {
40 if (c=='=') {
41 *t = '\0';
42 value = t+1;
43 } else if (c=='+') {
44 *t = ' ';
45 } else if (c=='%') {
46 t = convert_query_hexchar(t);
47 } else if (c=='&') {
48 *t = '\0';
49 (*fn)(txt, value);
50 txt = t+1;
51 value = NULL;
52 }
53 t++;
54 }
55 if (t!=txt)
56 (*fn)(txt, value);
57 return 0;
58}
59
60/* 11/*
61 * url syntax: [repo ['/' cmd [ '/' path]]] 12 * url syntax: [repo ['/' cmd [ '/' path]]]
62 * repo: any valid repo url, may contain '/' 13 * repo: any valid repo url, may contain '/'
63 * cmd: log | commit | diff | tree | view | blob | snapshot 14 * cmd: log | commit | diff | tree | view | blob | snapshot
64 * path: any valid path, may contain '/' 15 * path: any valid path, may contain '/'
65 * 16 *
66 */ 17 */
67void cgit_parse_url(const char *url) 18void cgit_parse_url(const char *url)
68{ 19{
69 char *cmd, *p; 20 char *cmd, *p;
70 21
71 ctx.repo = NULL; 22 ctx.repo = NULL;
72 if (!url || url[0] == '\0') 23 if (!url || url[0] == '\0')
73 return; 24 return;
74 25
75 ctx.repo = cgit_get_repoinfo(url); 26 ctx.repo = cgit_get_repoinfo(url);
76 if (ctx.repo) { 27 if (ctx.repo) {
77 ctx.qry.repo = ctx.repo->url; 28 ctx.qry.repo = ctx.repo->url;
78 return; 29 return;
79 } 30 }
80 31
81 cmd = strchr(url, '/'); 32 cmd = strchr(url, '/');
82 while (!ctx.repo && cmd) { 33 while (!ctx.repo && cmd) {
83 cmd[0] = '\0'; 34 cmd[0] = '\0';
84 ctx.repo = cgit_get_repoinfo(url); 35 ctx.repo = cgit_get_repoinfo(url);
85 if (ctx.repo == NULL) { 36 if (ctx.repo == NULL) {
86 cmd[0] = '/'; 37 cmd[0] = '/';
87 cmd = strchr(cmd + 1, '/'); 38 cmd = strchr(cmd + 1, '/');
88 continue; 39 continue;
89 } 40 }
90 41
91 ctx.qry.repo = ctx.repo->url; 42 ctx.qry.repo = ctx.repo->url;
92 p = strchr(cmd + 1, '/'); 43 p = strchr(cmd + 1, '/');
93 if (p) { 44 if (p) {
94 p[0] = '\0'; 45 p[0] = '\0';
95 if (p[1]) 46 if (p[1])
96 ctx.qry.path = trim_end(p + 1, '/'); 47 ctx.qry.path = trim_end(p + 1, '/');
97 } 48 }
98 if (cmd[1]) 49 if (cmd[1])
99 ctx.qry.page = xstrdup(cmd + 1); 50 ctx.qry.page = xstrdup(cmd + 1);
100 return; 51 return;
101 } 52 }
102} 53}
103 54
104char *substr(const char *head, const char *tail) 55char *substr(const char *head, const char *tail)
105{ 56{
106 char *buf; 57 char *buf;
107 58
108 buf = xmalloc(tail - head + 1); 59 buf = xmalloc(tail - head + 1);
109 strncpy(buf, head, tail - head); 60 strncpy(buf, head, tail - head);
110 buf[tail - head] = '\0'; 61 buf[tail - head] = '\0';
111 return buf; 62 return buf;
112} 63}
113 64
114struct commitinfo *cgit_parse_commit(struct commit *commit) 65struct commitinfo *cgit_parse_commit(struct commit *commit)
115{ 66{
116 struct commitinfo *ret; 67 struct commitinfo *ret;
117 char *p = commit->buffer, *t = commit->buffer; 68 char *p = commit->buffer, *t = commit->buffer;
118 69
119 ret = xmalloc(sizeof(*ret)); 70 ret = xmalloc(sizeof(*ret));
120 ret->commit = commit; 71 ret->commit = commit;
121 ret->author = NULL; 72 ret->author = NULL;
122 ret->author_email = NULL; 73 ret->author_email = NULL;
123 ret->committer = NULL; 74 ret->committer = NULL;
124 ret->committer_email = NULL; 75 ret->committer_email = NULL;
125 ret->subject = NULL; 76 ret->subject = NULL;
126 ret->msg = NULL; 77 ret->msg = NULL;
127 ret->msg_encoding = NULL; 78 ret->msg_encoding = NULL;
128 79
129 if (p == NULL) 80 if (p == NULL)
130 return ret; 81 return ret;
131 82
132 if (strncmp(p, "tree ", 5)) 83 if (strncmp(p, "tree ", 5))
133 die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); 84 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
134 else 85 else
135 p += 46; // "tree " + hex[40] + "\n" 86 p += 46; // "tree " + hex[40] + "\n"
136 87
137 while (!strncmp(p, "parent ", 7)) 88 while (!strncmp(p, "parent ", 7))
138 p += 48; // "parent " + hex[40] + "\n" 89 p += 48; // "parent " + hex[40] + "\n"
139 90
140 if (!strncmp(p, "author ", 7)) { 91 if (!strncmp(p, "author ", 7)) {
141 p += 7; 92 p += 7;
142 t = strchr(p, '<') - 1; 93 t = strchr(p, '<') - 1;
143 ret->author = substr(p, t); 94 ret->author = substr(p, t);
144 p = t; 95 p = t;
145 t = strchr(t, '>') + 1; 96 t = strchr(t, '>') + 1;
146 ret->author_email = substr(p, t); 97 ret->author_email = substr(p, t);
147 ret->author_date = atol(t+1); 98 ret->author_date = atol(t+1);
148 p = strchr(t, '\n') + 1; 99 p = strchr(t, '\n') + 1;
149 } 100 }
150 101
151 if (!strncmp(p, "committer ", 9)) { 102 if (!strncmp(p, "committer ", 9)) {
152 p += 9; 103 p += 9;
153 t = strchr(p, '<') - 1; 104 t = strchr(p, '<') - 1;
154 ret->committer = substr(p, t); 105 ret->committer = substr(p, t);
155 p = t; 106 p = t;
156 t = strchr(t, '>') + 1; 107 t = strchr(t, '>') + 1;
157 ret->committer_email = substr(p, t); 108 ret->committer_email = substr(p, t);
158 ret->committer_date = atol(t+1); 109 ret->committer_date = atol(t+1);
159 p = strchr(t, '\n') + 1; 110 p = strchr(t, '\n') + 1;
160 } 111 }
161 112
162 if (!strncmp(p, "encoding ", 9)) { 113 if (!strncmp(p, "encoding ", 9)) {
163 p += 9; 114 p += 9;
164 t = strchr(p, '\n') + 1; 115 t = strchr(p, '\n') + 1;
165 ret->msg_encoding = substr(p, t); 116 ret->msg_encoding = substr(p, t);
166 p = t; 117 p = t;
167 } else 118 } else
168 ret->msg_encoding = xstrdup(PAGE_ENCODING); 119 ret->msg_encoding = xstrdup(PAGE_ENCODING);
169 120
170 while (*p && (*p != '\n')) 121 while (*p && (*p != '\n'))
171 p = strchr(p, '\n') + 1; // skip unknown header fields 122 p = strchr(p, '\n') + 1; // skip unknown header fields
172 123
173 while (*p == '\n') 124 while (*p == '\n')
174 p = strchr(p, '\n') + 1; 125 p = strchr(p, '\n') + 1;
175 126
176 t = strchr(p, '\n'); 127 t = strchr(p, '\n');
177 if (t) { 128 if (t) {
178 if (*t == '\0') 129 if (*t == '\0')
179 ret->subject = "** empty **"; 130 ret->subject = "** empty **";
180 else 131 else
181 ret->subject = substr(p, t); 132 ret->subject = substr(p, t);
182 p = t + 1; 133 p = t + 1;
183 134
184 while (*p == '\n') 135 while (*p == '\n')
185 p = strchr(p, '\n') + 1; 136 p = strchr(p, '\n') + 1;
186 ret->msg = xstrdup(p); 137 ret->msg = xstrdup(p);
187 } else 138 } else
188 ret->subject = substr(p, p+strlen(p)); 139 ret->subject = substr(p, p+strlen(p));
189 140
190 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) { 141 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
191 t = reencode_string(ret->subject, PAGE_ENCODING, 142 t = reencode_string(ret->subject, PAGE_ENCODING,
192 ret->msg_encoding); 143 ret->msg_encoding);
193 if(t) { 144 if(t) {
194 free(ret->subject); 145 free(ret->subject);
195 ret->subject = t; 146 ret->subject = t;
196 } 147 }
197 148
198 t = reencode_string(ret->msg, PAGE_ENCODING, 149 t = reencode_string(ret->msg, PAGE_ENCODING,
199 ret->msg_encoding); 150 ret->msg_encoding);
200 if(t) { 151 if(t) {
201 free(ret->msg); 152 free(ret->msg);
202 ret->msg = t; 153 ret->msg = t;
203 } 154 }
204 } 155 }
205 156
206 return ret; 157 return ret;
207} 158}
208 159
209 160
210struct taginfo *cgit_parse_tag(struct tag *tag) 161struct taginfo *cgit_parse_tag(struct tag *tag)
211{ 162{
212 void *data; 163 void *data;
213 enum object_type type; 164 enum object_type type;
214 unsigned long size; 165 unsigned long size;
215 char *p, *t; 166 char *p, *t;
216 struct taginfo *ret; 167 struct taginfo *ret;
217 168
218 data = read_sha1_file(tag->object.sha1, &type, &size); 169 data = read_sha1_file(tag->object.sha1, &type, &size);
219 if (!data || type != OBJ_TAG) { 170 if (!data || type != OBJ_TAG) {
220 free(data); 171 free(data);
221 return 0; 172 return 0;
222 } 173 }
223 174
224 ret = xmalloc(sizeof(*ret)); 175 ret = xmalloc(sizeof(*ret));
225 ret->tagger = NULL; 176 ret->tagger = NULL;
226 ret->tagger_email = NULL; 177 ret->tagger_email = NULL;
227 ret->tagger_date = 0; 178 ret->tagger_date = 0;
228 ret->msg = NULL; 179 ret->msg = NULL;
229 180
230 p = data; 181 p = data;
231 182
232 while (p && *p) { 183 while (p && *p) {
233 if (*p == '\n') 184 if (*p == '\n')
234 break; 185 break;
235 186
236 if (!strncmp(p, "tagger ", 7)) { 187 if (!strncmp(p, "tagger ", 7)) {
237 p += 7; 188 p += 7;
238 t = strchr(p, '<') - 1; 189 t = strchr(p, '<') - 1;
239 ret->tagger = substr(p, t); 190 ret->tagger = substr(p, t);
240 p = t; 191 p = t;
241 t = strchr(t, '>') + 1; 192 t = strchr(t, '>') + 1;
242 ret->tagger_email = substr(p, t); 193 ret->tagger_email = substr(p, t);
243 ret->tagger_date = atol(t+1); 194 ret->tagger_date = atol(t+1);
244 } 195 }
245 p = strchr(p, '\n') + 1; 196 p = strchr(p, '\n') + 1;
246 } 197 }
247 198
248 while (p && *p && (*p != '\n')) 199 while (p && *p && (*p != '\n'))
249 p = strchr(p, '\n') + 1; // skip unknown tag fields 200 p = strchr(p, '\n') + 1; // skip unknown tag fields
250 201
251 while (p && (*p == '\n')) 202 while (p && (*p == '\n'))
252 p = strchr(p, '\n') + 1; 203 p = strchr(p, '\n') + 1;
253 if (p && *p) 204 if (p && *p)
254 ret->msg = xstrdup(p); 205 ret->msg = xstrdup(p);
255 free(data); 206 free(data);
256 return ret; 207 return ret;
257} 208}