c287ec50c506a40fd67b6dc3fdbf582144d7e813
[project/luci.git] / libs / sgi-webuci / boa-patches / 200-plugin_api.patch
1 Index: boa-0.94.13/src/list.h
2 ===================================================================
3 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
4 +++ boa-0.94.13/src/list.h      2008-06-29 01:12:36.000000000 +0200
5 @@ -0,0 +1,601 @@
6 +#ifndef _LINUX_LIST_H
7 +#define _LINUX_LIST_H
8 +
9 +#include <stddef.h>
10 +/**
11 + * container_of - cast a member of a structure out to the containing structure
12 + * @ptr:       the pointer to the member.
13 + * @type:      the type of the container struct this is embedded in.
14 + * @member:    the name of the member within the struct.
15 + *
16 + */
17 +#ifndef container_of
18 +#define container_of(ptr, type, member) (                      \
19 +       (type *)( (char *)ptr - offsetof(type,member) ))
20 +#endif
21 +
22 +
23 +/*
24 + * Simple doubly linked list implementation.
25 + *
26 + * Some of the internal functions ("__xxx") are useful when
27 + * manipulating whole lists rather than single entries, as
28 + * sometimes we already know the next/prev entries and we can
29 + * generate better code by using them directly rather than
30 + * using the generic single-entry routines.
31 + */
32 +
33 +struct list_head {
34 +       struct list_head *next, *prev;
35 +};
36 +
37 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
38 +
39 +#define LIST_HEAD(name) \
40 +       struct list_head name = LIST_HEAD_INIT(name)
41 +
42 +static inline void INIT_LIST_HEAD(struct list_head *list)
43 +{
44 +       list->next = list;
45 +       list->prev = list;
46 +}
47 +
48 +/*
49 + * Insert a new entry between two known consecutive entries.
50 + *
51 + * This is only for internal list manipulation where we know
52 + * the prev/next entries already!
53 + */
54 +static inline void __list_add(struct list_head *new,
55 +                             struct list_head *prev,
56 +                             struct list_head *next)
57 +{
58 +       next->prev = new;
59 +       new->next = next;
60 +       new->prev = prev;
61 +       prev->next = new;
62 +}
63 +
64 +/**
65 + * list_add - add a new entry
66 + * @new: new entry to be added
67 + * @head: list head to add it after
68 + *
69 + * Insert a new entry after the specified head.
70 + * This is good for implementing stacks.
71 + */
72 +static inline void list_add(struct list_head *new, struct list_head *head)
73 +{
74 +       __list_add(new, head, head->next);
75 +}
76 +
77 +
78 +/**
79 + * list_add_tail - add a new entry
80 + * @new: new entry to be added
81 + * @head: list head to add it before
82 + *
83 + * Insert a new entry before the specified head.
84 + * This is useful for implementing queues.
85 + */
86 +static inline void list_add_tail(struct list_head *new, struct list_head *head)
87 +{
88 +       __list_add(new, head->prev, head);
89 +}
90 +
91 +
92 +/*
93 + * Delete a list entry by making the prev/next entries
94 + * point to each other.
95 + *
96 + * This is only for internal list manipulation where we know
97 + * the prev/next entries already!
98 + */
99 +static inline void __list_del(struct list_head * prev, struct list_head * next)
100 +{
101 +       next->prev = prev;
102 +       prev->next = next;
103 +}
104 +
105 +/**
106 + * list_del - deletes entry from list.
107 + * @entry: the element to delete from the list.
108 + * Note: list_empty() on entry does not return true after this, the entry is
109 + * in an undefined state.
110 + */
111 +static inline void list_del(struct list_head *entry)
112 +{
113 +       __list_del(entry->prev, entry->next);
114 +       entry->next = NULL;
115 +       entry->prev = NULL;
116 +}
117 +
118 +/**
119 + * list_replace - replace old entry by new one
120 + * @old : the element to be replaced
121 + * @new : the new element to insert
122 + *
123 + * If @old was empty, it will be overwritten.
124 + */
125 +static inline void list_replace(struct list_head *old,
126 +                               struct list_head *new)
127 +{
128 +       new->next = old->next;
129 +       new->next->prev = new;
130 +       new->prev = old->prev;
131 +       new->prev->next = new;
132 +}
133 +
134 +static inline void list_replace_init(struct list_head *old,
135 +                                       struct list_head *new)
136 +{
137 +       list_replace(old, new);
138 +       INIT_LIST_HEAD(old);
139 +}
140 +
141 +/**
142 + * list_del_init - deletes entry from list and reinitialize it.
143 + * @entry: the element to delete from the list.
144 + */
145 +static inline void list_del_init(struct list_head *entry)
146 +{
147 +       __list_del(entry->prev, entry->next);
148 +       INIT_LIST_HEAD(entry);
149 +}
150 +
151 +/**
152 + * list_move - delete from one list and add as another's head
153 + * @list: the entry to move
154 + * @head: the head that will precede our entry
155 + */
156 +static inline void list_move(struct list_head *list, struct list_head *head)
157 +{
158 +       __list_del(list->prev, list->next);
159 +       list_add(list, head);
160 +}
161 +
162 +/**
163 + * list_move_tail - delete from one list and add as another's tail
164 + * @list: the entry to move
165 + * @head: the head that will follow our entry
166 + */
167 +static inline void list_move_tail(struct list_head *list,
168 +                                 struct list_head *head)
169 +{
170 +       __list_del(list->prev, list->next);
171 +       list_add_tail(list, head);
172 +}
173 +
174 +/**
175 + * list_is_last - tests whether @list is the last entry in list @head
176 + * @list: the entry to test
177 + * @head: the head of the list
178 + */
179 +static inline int list_is_last(const struct list_head *list,
180 +                               const struct list_head *head)
181 +{
182 +       return list->next == head;
183 +}
184 +
185 +/**
186 + * list_empty - tests whether a list is empty
187 + * @head: the list to test.
188 + */
189 +static inline int list_empty(const struct list_head *head)
190 +{
191 +       return head->next == head;
192 +}
193 +
194 +/**
195 + * list_empty_careful - tests whether a list is empty and not being modified
196 + * @head: the list to test
197 + *
198 + * Description:
199 + * tests whether a list is empty _and_ checks that no other CPU might be
200 + * in the process of modifying either member (next or prev)
201 + *
202 + * NOTE: using list_empty_careful() without synchronization
203 + * can only be safe if the only activity that can happen
204 + * to the list entry is list_del_init(). Eg. it cannot be used
205 + * if another CPU could re-list_add() it.
206 + */
207 +static inline int list_empty_careful(const struct list_head *head)
208 +{
209 +       struct list_head *next = head->next;
210 +       return (next == head) && (next == head->prev);
211 +}
212 +
213 +static inline void __list_splice(struct list_head *list,
214 +                                struct list_head *head)
215 +{
216 +       struct list_head *first = list->next;
217 +       struct list_head *last = list->prev;
218 +       struct list_head *at = head->next;
219 +
220 +       first->prev = head;
221 +       head->next = first;
222 +
223 +       last->next = at;
224 +       at->prev = last;
225 +}
226 +
227 +/**
228 + * list_splice - join two lists
229 + * @list: the new list to add.
230 + * @head: the place to add it in the first list.
231 + */
232 +static inline void list_splice(struct list_head *list, struct list_head *head)
233 +{
234 +       if (!list_empty(list))
235 +               __list_splice(list, head);
236 +}
237 +
238 +/**
239 + * list_splice_init - join two lists and reinitialise the emptied list.
240 + * @list: the new list to add.
241 + * @head: the place to add it in the first list.
242 + *
243 + * The list at @list is reinitialised
244 + */
245 +static inline void list_splice_init(struct list_head *list,
246 +                                   struct list_head *head)
247 +{
248 +       if (!list_empty(list)) {
249 +               __list_splice(list, head);
250 +               INIT_LIST_HEAD(list);
251 +       }
252 +}
253 +
254 +/**
255 + * list_entry - get the struct for this entry
256 + * @ptr:       the &struct list_head pointer.
257 + * @type:      the type of the struct this is embedded in.
258 + * @member:    the name of the list_struct within the struct.
259 + */
260 +#define list_entry(ptr, type, member) \
261 +       container_of(ptr, type, member)
262 +
263 +/**
264 + * list_first_entry - get the first element from a list
265 + * @ptr:       the list head to take the element from.
266 + * @type:      the type of the struct this is embedded in.
267 + * @member:    the name of the list_struct within the struct.
268 + *
269 + * Note, that list is expected to be not empty.
270 + */
271 +#define list_first_entry(ptr, type, member) \
272 +       list_entry((ptr)->next, type, member)
273 +
274 +/**
275 + * list_for_each       -       iterate over a list
276 + * @pos:       the &struct list_head to use as a loop cursor.
277 + * @head:      the head for your list.
278 + */
279 +#define list_for_each(pos, head) \
280 +       for (pos = (head)->next; pos != (head); \
281 +               pos = pos->next)
282 +
283 +/**
284 + * __list_for_each     -       iterate over a list
285 + * @pos:       the &struct list_head to use as a loop cursor.
286 + * @head:      the head for your list.
287 + *
288 + * This variant differs from list_for_each() in that it's the
289 + * simplest possible list iteration code, no prefetching is done.
290 + * Use this for code that knows the list to be very short (empty
291 + * or 1 entry) most of the time.
292 + */
293 +#define __list_for_each(pos, head) \
294 +       for (pos = (head)->next; pos != (head); pos = pos->next)
295 +
296 +/**
297 + * list_for_each_prev  -       iterate over a list backwards
298 + * @pos:       the &struct list_head to use as a loop cursor.
299 + * @head:      the head for your list.
300 + */
301 +#define list_for_each_prev(pos, head) \
302 +       for (pos = (head)->prev; pos != (head); \
303 +               pos = pos->prev)
304 +
305 +/**
306 + * list_for_each_safe - iterate over a list safe against removal of list entry
307 + * @pos:       the &struct list_head to use as a loop cursor.
308 + * @n:         another &struct list_head to use as temporary storage
309 + * @head:      the head for your list.
310 + */
311 +#define list_for_each_safe(pos, n, head) \
312 +       for (pos = (head)->next, n = pos->next; pos != (head); \
313 +               pos = n, n = pos->next)
314 +
315 +/**
316 + * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
317 + * @pos:       the &struct list_head to use as a loop cursor.
318 + * @n:         another &struct list_head to use as temporary storage
319 + * @head:      the head for your list.
320 + */
321 +#define list_for_each_prev_safe(pos, n, head) \
322 +       for (pos = (head)->prev, n = pos->prev; \
323 +            pos != (head); \
324 +            pos = n, n = pos->prev)
325 +
326 +/**
327 + * list_for_each_entry -       iterate over list of given type
328 + * @pos:       the type * to use as a loop cursor.
329 + * @head:      the head for your list.
330 + * @member:    the name of the list_struct within the struct.
331 + */
332 +#define list_for_each_entry(pos, head, member)                         \
333 +       for (pos = list_entry((head)->next, typeof(*pos), member);      \
334 +            &pos->member != (head);    \
335 +            pos = list_entry(pos->member.next, typeof(*pos), member))
336 +
337 +/**
338 + * list_for_each_entry_reverse - iterate backwards over list of given type.
339 + * @pos:       the type * to use as a loop cursor.
340 + * @head:      the head for your list.
341 + * @member:    the name of the list_struct within the struct.
342 + */
343 +#define list_for_each_entry_reverse(pos, head, member)                 \
344 +       for (pos = list_entry((head)->prev, typeof(*pos), member);      \
345 +            &pos->member != (head);    \
346 +            pos = list_entry(pos->member.prev, typeof(*pos), member))
347 +
348 +/**
349 + * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
350 + * @pos:       the type * to use as a start point
351 + * @head:      the head of the list
352 + * @member:    the name of the list_struct within the struct.
353 + *
354 + * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
355 + */
356 +#define list_prepare_entry(pos, head, member) \
357 +       ((pos) ? : list_entry(head, typeof(*pos), member))
358 +
359 +/**
360 + * list_for_each_entry_continue - continue iteration over list of given type
361 + * @pos:       the type * to use as a loop cursor.
362 + * @head:      the head for your list.
363 + * @member:    the name of the list_struct within the struct.
364 + *
365 + * Continue to iterate over list of given type, continuing after
366 + * the current position.
367 + */
368 +#define list_for_each_entry_continue(pos, head, member)                \
369 +       for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
370 +            &pos->member != (head);    \
371 +            pos = list_entry(pos->member.next, typeof(*pos), member))
372 +
373 +/**
374 + * list_for_each_entry_continue_reverse - iterate backwards from the given point
375 + * @pos:       the type * to use as a loop cursor.
376 + * @head:      the head for your list.
377 + * @member:    the name of the list_struct within the struct.
378 + *
379 + * Start to iterate over list of given type backwards, continuing after
380 + * the current position.
381 + */
382 +#define list_for_each_entry_continue_reverse(pos, head, member)                \
383 +       for (pos = list_entry(pos->member.prev, typeof(*pos), member);  \
384 +            &pos->member != (head);    \
385 +            pos = list_entry(pos->member.prev, typeof(*pos), member))
386 +
387 +/**
388 + * list_for_each_entry_from - iterate over list of given type from the current point
389 + * @pos:       the type * to use as a loop cursor.
390 + * @head:      the head for your list.
391 + * @member:    the name of the list_struct within the struct.
392 + *
393 + * Iterate over list of given type, continuing from current position.
394 + */
395 +#define list_for_each_entry_from(pos, head, member)                    \
396 +       for (; &pos->member != (head);  \
397 +            pos = list_entry(pos->member.next, typeof(*pos), member))
398 +
399 +/**
400 + * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
401 + * @pos:       the type * to use as a loop cursor.
402 + * @n:         another type * to use as temporary storage
403 + * @head:      the head for your list.
404 + * @member:    the name of the list_struct within the struct.
405 + */
406 +#define list_for_each_entry_safe(pos, n, head, member)                 \
407 +       for (pos = list_entry((head)->next, typeof(*pos), member),      \
408 +               n = list_entry(pos->member.next, typeof(*pos), member); \
409 +            &pos->member != (head);                                    \
410 +            pos = n, n = list_entry(n->member.next, typeof(*n), member))
411 +
412 +/**
413 + * list_for_each_entry_safe_continue
414 + * @pos:       the type * to use as a loop cursor.
415 + * @n:         another type * to use as temporary storage
416 + * @head:      the head for your list.
417 + * @member:    the name of the list_struct within the struct.
418 + *
419 + * Iterate over list of given type, continuing after current point,
420 + * safe against removal of list entry.
421 + */
422 +#define list_for_each_entry_safe_continue(pos, n, head, member)                \
423 +       for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
424 +               n = list_entry(pos->member.next, typeof(*pos), member);         \
425 +            &pos->member != (head);                                            \
426 +            pos = n, n = list_entry(n->member.next, typeof(*n), member))
427 +
428 +/**
429 + * list_for_each_entry_safe_from
430 + * @pos:       the type * to use as a loop cursor.
431 + * @n:         another type * to use as temporary storage
432 + * @head:      the head for your list.
433 + * @member:    the name of the list_struct within the struct.
434 + *
435 + * Iterate over list of given type from current point, safe against
436 + * removal of list entry.
437 + */
438 +#define list_for_each_entry_safe_from(pos, n, head, member)                    \
439 +       for (n = list_entry(pos->member.next, typeof(*pos), member);            \
440 +            &pos->member != (head);                                            \
441 +            pos = n, n = list_entry(n->member.next, typeof(*n), member))
442 +
443 +/**
444 + * list_for_each_entry_safe_reverse
445 + * @pos:       the type * to use as a loop cursor.
446 + * @n:         another type * to use as temporary storage
447 + * @head:      the head for your list.
448 + * @member:    the name of the list_struct within the struct.
449 + *
450 + * Iterate backwards over list of given type, safe against removal
451 + * of list entry.
452 + */
453 +#define list_for_each_entry_safe_reverse(pos, n, head, member)         \
454 +       for (pos = list_entry((head)->prev, typeof(*pos), member),      \
455 +               n = list_entry(pos->member.prev, typeof(*pos), member); \
456 +            &pos->member != (head);                                    \
457 +            pos = n, n = list_entry(n->member.prev, typeof(*n), member))
458 +
459 +/*
460 + * Double linked lists with a single pointer list head.
461 + * Mostly useful for hash tables where the two pointer list head is
462 + * too wasteful.
463 + * You lose the ability to access the tail in O(1).
464 + */
465 +
466 +struct hlist_head {
467 +       struct hlist_node *first;
468 +};
469 +
470 +struct hlist_node {
471 +       struct hlist_node *next, **pprev;
472 +};
473 +
474 +#define HLIST_HEAD_INIT { .first = NULL }
475 +#define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
476 +#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
477 +static inline void INIT_HLIST_NODE(struct hlist_node *h)
478 +{
479 +       h->next = NULL;
480 +       h->pprev = NULL;
481 +}
482 +
483 +static inline int hlist_unhashed(const struct hlist_node *h)
484 +{
485 +       return !h->pprev;
486 +}
487 +
488 +static inline int hlist_empty(const struct hlist_head *h)
489 +{
490 +       return !h->first;
491 +}
492 +
493 +static inline void __hlist_del(struct hlist_node *n)
494 +{
495 +       struct hlist_node *next = n->next;
496 +       struct hlist_node **pprev = n->pprev;
497 +       *pprev = next;
498 +       if (next)
499 +               next->pprev = pprev;
500 +}
501 +
502 +static inline void hlist_del(struct hlist_node *n)
503 +{
504 +       __hlist_del(n);
505 +       n->next = NULL;
506 +       n->pprev = NULL;
507 +}
508 +
509 +static inline void hlist_del_init(struct hlist_node *n)
510 +{
511 +       if (!hlist_unhashed(n)) {
512 +               __hlist_del(n);
513 +               INIT_HLIST_NODE(n);
514 +       }
515 +}
516 +
517 +
518 +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
519 +{
520 +       struct hlist_node *first = h->first;
521 +       n->next = first;
522 +       if (first)
523 +               first->pprev = &n->next;
524 +       h->first = n;
525 +       n->pprev = &h->first;
526 +}
527 +
528 +
529 +/* next must be != NULL */
530 +static inline void hlist_add_before(struct hlist_node *n,
531 +                                       struct hlist_node *next)
532 +{
533 +       n->pprev = next->pprev;
534 +       n->next = next;
535 +       next->pprev = &n->next;
536 +       *(n->pprev) = n;
537 +}
538 +
539 +static inline void hlist_add_after(struct hlist_node *n,
540 +                                       struct hlist_node *next)
541 +{
542 +       next->next = n->next;
543 +       n->next = next;
544 +       next->pprev = &n->next;
545 +
546 +       if(next->next)
547 +               next->next->pprev  = &next->next;
548 +}
549 +
550 +#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
551 +
552 +#define hlist_for_each(pos, head) \
553 +       for (pos = (head)->first; pos; pos = pos->next)
554 +
555 +#define hlist_for_each_safe(pos, n, head) \
556 +       for (pos = (head)->first; pos; pos = n)
557 +
558 +/**
559 + * hlist_for_each_entry        - iterate over list of given type
560 + * @tpos:      the type * to use as a loop cursor.
561 + * @pos:       the &struct hlist_node to use as a loop cursor.
562 + * @head:      the head for your list.
563 + * @member:    the name of the hlist_node within the struct.
564 + */
565 +#define hlist_for_each_entry(tpos, pos, head, member)                   \
566 +       for (pos = (head)->first; pos &&                                 \
567 +               ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
568 +            pos = pos->next)
569 +
570 +/**
571 + * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
572 + * @tpos:      the type * to use as a loop cursor.
573 + * @pos:       the &struct hlist_node to use as a loop cursor.
574 + * @member:    the name of the hlist_node within the struct.
575 + */
576 +#define hlist_for_each_entry_continue(tpos, pos, member)               \
577 +       for (pos = (pos)->next; pos &&                                  \
578 +            ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});   \
579 +            pos = pos->next)
580 +
581 +/**
582 + * hlist_for_each_entry_from - iterate over a hlist continuing from current point
583 + * @tpos:      the type * to use as a loop cursor.
584 + * @pos:       the &struct hlist_node to use as a loop cursor.
585 + * @member:    the name of the hlist_node within the struct.
586 + */
587 +#define hlist_for_each_entry_from(tpos, pos, member)                    \
588 +       for (; pos &&                    \
589 +               ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
590 +            pos = pos->next)
591 +
592 +/**
593 + * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
594 + * @tpos:      the type * to use as a loop cursor.
595 + * @pos:       the &struct hlist_node to use as a loop cursor.
596 + * @n:         another &struct hlist_node to use as temporary storage
597 + * @head:      the head for your list.
598 + * @member:    the name of the hlist_node within the struct.
599 + */
600 +#define hlist_for_each_entry_safe(tpos, pos, n, head, member)           \
601 +       for (pos = (head)->first;                                        \
602 +            pos && ({ n = pos->next; 1; }) &&                           \
603 +               ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
604 +            pos = n)
605 +
606 +#endif
607 Index: boa-0.94.13/src/plugin.c
608 ===================================================================
609 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
610 +++ boa-0.94.13/src/plugin.c    2008-06-29 02:02:42.000000000 +0200
611 @@ -0,0 +1,191 @@
612 +/*
613 + * Simple plugin API for boa
614 + * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
615 + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
616 + *
617 + * This program is free software; you can redistribute it and/or modify
618 + * it under the terms of the GNU General Public License version 2
619 + * as published by the Free Software Foundation.
620 + *
621 + * This program is distributed in the hope that it will be useful,
622 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
623 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
624 + * GNU General Public License for more details.
625 + *
626 + * You should have received a copy of the GNU General Public License
627 + * along with this program; if not, write to the Free Software
628 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
629 + */
630 +
631 +#include "boa.h"
632 +#include "list.h"
633 +#include <dlfcn.h>
634 +#include <glob.h>
635 +
636 +static LIST_HEAD(plugins);
637 +
638 +struct httpd_plugin *plugin_lookup(request *req)
639 +{
640 +       struct list_head *l;
641 +       list_for_each(l, &plugins)
642 +       {
643 +               struct httpd_plugin *p =
644 +                       container_of(l, struct httpd_plugin, list);
645 +
646 +               if (!strncmp(req->request_uri, p->prefix, strlen(p->prefix)))
647 +                       return p;
648 +       }
649 +       return NULL;
650 +}
651 +
652 +static int plugin_run(request *req, struct httpd_plugin *p)
653 +{
654 +       struct http_context ctx;
655 +    int child_pid;
656 +
657 +       SQUASH_KA(req);
658 +
659 +       memset(&ctx, 0, sizeof(ctx));
660 +       ctx.uri = req->request_uri;
661 +       switch(req->method) {
662 +               case M_POST:
663 +                       ctx.request_method = "POST";
664 +                       break;
665 +               case M_HEAD:
666 +                       ctx.request_method = "HEAD";
667 +                       break;
668 +               case M_GET:
669 +                       ctx.request_method = "GET";
670 +                       break;
671 +       }
672 +       ctx.server_addr = req->local_ip_addr;
673 +       ctx.server_proto = req->http_version;
674 +       ctx.query_string = req->query_string;
675 +       ctx.remote_addr = req->remote_ip_addr;
676 +       ctx.remote_port = req->remote_port;
677 +       ctx.cookie = req->cookie;
678 +       if (req->method == M_POST) {
679 +               if (req->content_type)
680 +                       ctx.content_type = req->content_type;
681 +               else
682 +                       ctx.content_type = default_type;
683 +               ctx.content_length = req->content_length;
684 +       }
685 +#ifdef ACCEPT_ON
686 +       if (req->accept[0])
687 +               ctx.http_accept = req->accept;
688 +#endif
689 +
690 +    p->prepare_req(p, &ctx);
691 +       child_pid = fork();
692 +
693 +       switch(child_pid) {
694 +    case -1:
695 +        log_error_time();
696 +        perror("fork");
697 +        send_r_error(req);
698 +        return 0;
699 +
700 +       case 0:
701 +        if (dup2(req->fd, STDOUT_FILENO) == -1) {
702 +            log_error_time();
703 +            perror("dup2 - fd");
704 +            _exit(1);
705 +        }
706 +        if (set_block_fd(req->fd) == -1) {
707 +            log_error_time();
708 +            perror("cgi-fcntl");
709 +            _exit(1);
710 +        }
711 +        if (req->method == M_POST) {
712 +            dup2(req->read_data_fd, STDIN_FILENO);
713 +            close(req->read_data_fd);
714 +            close(req->post_data_fd);
715 +                       set_block_fd(STDIN_FILENO);
716 +        }
717 +        close_access_log();
718 +
719 +        if (cgi_log_fd)
720 +            dup2(cgi_log_fd, STDERR_FILENO);
721 +
722 +           p->handle_req(p, &ctx);
723 +               exit(0);
724 +        break;
725 +    }
726 +
727 +    return 1;
728 +}
729 +
730 +int plugin_handle(request * req)
731 +{
732 +       struct httpd_plugin *p;
733 +
734 +       p = plugin_lookup(req);
735 +       if (!p)
736 +               return 0;
737 +
738 +       return plugin_run(req, p);
739 +}
740 +
741 +static void plugin_load(const char *p, const char *dir)
742 +{
743 +       struct httpd_plugin *plugin;
744 +       void *dl;
745 +
746 +       /* ignore directories */
747 +       if (p[strlen(p) - 1] == '/')
748 +               return;
749 +
750 +       dl = dlopen(p, RTLD_NOW | RTLD_GLOBAL);
751 +       if (!dl) {
752 +               fprintf(stderr, "Unable to load plugin '%s': %d\n", p, dlerror());
753 +               return;
754 +       }
755 +
756 +       plugin = dlsym(dl, "httpd_plugin");
757 +       if (!plugin)
758 +               goto error;
759 +
760 +       INIT_LIST_HEAD(&plugin->list);
761 +       plugin->dir = dir;
762 +
763 +       if (plugin->init(plugin) != 1)
764 +               goto error;
765 +
766 +       if (!plugin->prefix)
767 +               goto error_init;
768 +
769 +       list_add(&plugin->list, &plugins);
770 +       return;
771 +
772 +error_init:
773 +       plugin->done(plugin);
774 +error:
775 +       fprintf(stderr, "Plugin '%s' failed to initialize\n", p);
776 +       dlclose(dl);
777 +}
778 +
779 +#define WILDCARD_SUFFIX "/*.so"
780 +
781 +int plugin_init(char *path)
782 +{
783 +       int buflen = 128;
784 +       char *plugindir;
785 +       glob_t g;
786 +       char *s;
787 +       int i;
788 +
789 +       s = malloc(strlen(path) + sizeof(WILDCARD_SUFFIX) + 1);
790 +       strcpy(s, path);
791 +       strcat(s, WILDCARD_SUFFIX);
792 +       glob(s, GLOB_MARK, NULL, &g);
793 +       free(s);
794 +
795 +       for (i = 0; i < g.gl_pathc; i++)
796 +               plugin_load(g.gl_pathv[i], path);
797 +
798 +       globfree(&g);
799 +       return 1;
800 +}
801 +
802 +
803 Index: boa-0.94.13/src/request.c
804 ===================================================================
805 --- boa-0.94.13.orig/src/request.c      2008-06-29 01:11:52.000000000 +0200
806 +++ boa-0.94.13/src/request.c   2008-06-29 01:49:46.000000000 +0200
807 @@ -50,6 +50,7 @@
808          dequeue(&request_free, request_free); /* dequeue the head */
809      } else {
810          req = (request *) malloc(sizeof (request));
811 +               memset(req, 0, sizeof(request));
812          if (!req) {
813              log_error_time();
814              perror("malloc for new request");
815 @@ -603,6 +604,8 @@
816  
817  int process_header_end(request * req)
818  {
819 +       int ret;
820 +
821      if (!req->logline) {
822          send_r_error(req);
823          return 0;
824 @@ -630,11 +633,26 @@
825      }
826  
827      if (req->method == M_POST) {
828 -        req->post_data_fd = create_temporary_file(1, NULL, 0);
829 -        if (req->post_data_fd == 0)
830 -            return(0);
831 -        return(1); /* success */
832 -    }
833 +               if (!req->plugin) {
834 +               req->post_data_fd = create_temporary_file(1, NULL, 0);
835 +               } else {
836 +                       int fd[2];
837 +                       if (pipe(&fd[0]) != -1) {
838 +                               req->post_data_fd = fd[1];
839 +                               req->read_data_fd = fd[0];
840 +                               set_nonblock_fd(req->post_data_fd);
841 +                       }
842 +               }
843 +               if (req->post_data_fd == 0) {
844 +                       return(0);
845 +               }
846 +               if (!req->plugin)
847 +               return(1); /* success */
848 +    }
849 +
850 +       ret = plugin_handle(req);
851 +       if (ret)
852 +               return ret;
853  
854      if (req->is_cgi) {
855          return init_cgi(req);
856 @@ -698,6 +716,8 @@
857          req->header_user_agent = value;
858          if (!add_cgi_env(req, "USER_AGENT", value, 1))
859              return 0;
860 +       } else if (!memcmp(line, "COOKIE", 7)) {
861 +               req->cookie = value;
862      } else {
863          if (!add_cgi_env(req, line, value, 1))
864              return 0;
865 Index: boa-0.94.13/src/Makefile.in
866 ===================================================================
867 --- boa-0.94.13.orig/src/Makefile.in    2008-06-29 01:11:52.000000000 +0200
868 +++ boa-0.94.13/src/Makefile.in 2008-06-29 01:12:36.000000000 +0200
869 @@ -20,7 +20,7 @@
870  srcdir = @srcdir@
871  VPATH = @srcdir@:@srcdir@/../extras
872  LDFLAGS = @LDFLAGS@
873 -LIBS = @LIBS@
874 +LIBS = @LIBS@ -ldl
875  CFLAGS = @CFLAGS@ -I.
876  
877  # Change these if necessary
878 @@ -32,7 +32,8 @@
879  
880  SOURCES = alias.c boa.c buffer.c cgi.c cgi_header.c config.c escape.c \
881         get.c hash.c ip.c log.c mmap_cache.c pipe.c queue.c read.c \
882 -       request.c response.c select.c signals.c util.c sublog.c
883 +       request.c response.c select.c signals.c util.c sublog.c \
884 +       plugin.c
885  
886  OBJS = y.tab.o lex.yy.o $(SOURCES:.c=.o) timestamp.o @STRUTIL@
887  
888 Index: boa-0.94.13/src/boa.h
889 ===================================================================
890 --- boa-0.94.13.orig/src/boa.h  2008-06-29 01:11:52.000000000 +0200
891 +++ boa-0.94.13/src/boa.h       2008-06-29 01:12:36.000000000 +0200
892 @@ -37,6 +37,7 @@
893  #include <fcntl.h>
894  #include <limits.h>             /* OPEN_MAX */
895  #include <setjmp.h>
896 +#include <stdbool.h>
897  
898  #include <netdb.h>
899  #include <netinet/in.h>
900 @@ -50,6 +51,7 @@
901  #include "compat.h"             /* oh what fun is porting */
902  #include "defines.h"
903  #include "globals.h"
904 +#include "boa-plugin.h"
905  
906  /* alias */
907  void add_alias(char *fakename, char *realname, int script);
908 @@ -192,4 +194,9 @@
909  /* select */
910  void select_loop(int server_s);
911  
912 +/* plugins */
913 +int plugin_init(char *path);
914 +int plugin_handle(request * req);
915 +struct httpd_plugin *plugin_lookup(request *req);
916 +
917  #endif
918 Index: boa-0.94.13/src/config.c
919 ===================================================================
920 --- boa-0.94.13.orig/src/config.c       2008-06-29 01:11:52.000000000 +0200
921 +++ boa-0.94.13/src/config.c    2008-06-29 01:12:36.000000000 +0200
922 @@ -61,6 +61,7 @@
923  char *error_log_name;
924  char *access_log_name;
925  char *cgi_log_name;
926 +char *plugin_root = NULL;
927  
928  int use_localtime;
929  
930 @@ -116,6 +117,7 @@
931      {"SinglePostLimit", S1A, c_set_int, &single_post_limit},
932      {"CGIPath", S1A, c_set_string, &cgi_path},
933      {"MaxConnections", S1A, c_set_int, &max_connections},
934 +    {"PluginRoot", S1A, c_set_string, &plugin_root},
935  };
936  
937  static void c_set_user(char *v1, char *v2, void *t)
938 @@ -323,6 +325,22 @@
939          free(dirmaker);
940          dirmaker = temp;
941      }
942 +       if (plugin_root) {
943 +               char *plugin_path = plugin_root;
944 +               char *next;
945 +
946 +               do {
947 +                       next = strchr(plugin_path, ':');
948 +                       if (next) {
949 +                               *next = 0;
950 +                               next++;
951 +                       }
952 +
953 +                       plugin_init(normalize_path(plugin_path));
954 +                       plugin_path = next;
955 +               } while (plugin_path);
956 +               free(plugin_root);
957 +       }
958  
959  #if 0
960      if (mime_types) {
961 Index: boa-0.94.13/src/alias.c
962 ===================================================================
963 --- boa-0.94.13.orig/src/alias.c        2008-06-29 01:11:52.000000000 +0200
964 +++ boa-0.94.13/src/alias.c     2008-06-29 01:12:36.000000000 +0200
965 @@ -213,6 +213,7 @@
966      uri_len = strlen(req->request_uri);
967  
968      current = find_alias(req->request_uri, uri_len);
969 +       req->plugin = !!plugin_lookup(req);
970      if (current) {
971  
972          if (current->type == SCRIPTALIAS) /* Script */
973 @@ -237,7 +238,7 @@
974          }
975  
976          if (current->type == REDIRECT) { /* Redirect */
977 -            if (req->method == M_POST) { /* POST to non-script */
978 +            if ((req->method == M_POST) && !req->plugin) { /* POST to non-script */
979                  /* it's not a cgi, but we try to POST??? */
980                  send_r_bad_request(req);
981                  return 0;       /* not a script alias, therefore begin filling in data */
982 @@ -361,7 +362,7 @@
983          else
984              req->is_cgi = CGI;
985          return 1;
986 -    } else if (req->method == M_POST) { /* POST to non-script */
987 +    } else if ((req->method == M_POST) && !req->plugin) { /* POST to non-script */
988          /* it's not a cgi, but we try to POST??? */
989          send_r_bad_request(req);
990          return 0;
991 Index: boa-0.94.13/src/globals.h
992 ===================================================================
993 --- boa-0.94.13.orig/src/globals.h      2008-06-29 01:11:52.000000000 +0200
994 +++ boa-0.94.13/src/globals.h   2008-06-29 01:47:25.000000000 +0200
995 @@ -47,6 +47,7 @@
996  struct request {                /* pending requests */
997      int fd;                     /* client's socket fd */
998      int status;                 /* see #defines.h */
999 +    bool plugin;
1000      time_t time_last;           /* time of last succ. op. */
1001      char *pathname;             /* pathname of requested file */
1002      int simple;                 /* simple request? */
1003 @@ -92,6 +93,7 @@
1004      char *header_referer;
1005  
1006      int post_data_fd;           /* fd for post data tmpfile */
1007 +    int read_data_fd;           /* fd for post data input (plugin) */
1008  
1009      char *path_info;            /* env variable */
1010      char *path_translated;      /* env variable */
1011 @@ -99,6 +101,7 @@
1012      char *query_string;         /* env variable */
1013      char *content_type;         /* env variable */
1014      char *content_length;       /* env variable */
1015 +       char *cookie;               /* env variable */
1016  
1017      struct mmap_entry *mmap_entry_var;
1018  
1019 Index: boa-0.94.13/src/read.c
1020 ===================================================================
1021 --- boa-0.94.13.orig/src/read.c 2008-06-29 01:11:52.000000000 +0200
1022 +++ boa-0.94.13/src/read.c      2008-06-29 01:12:36.000000000 +0200
1023 @@ -338,8 +338,11 @@
1024  
1025      if (bytes_to_write == 0) {  /* nothing left in buffer to write */
1026          req->header_line = req->header_end = req->buffer;
1027 -        if (req->filepos >= req->filesize)
1028 -            return init_cgi(req);
1029 +        if (req->filepos >= req->filesize) {
1030 +                       if (req->post_data_fd > 0)
1031 +                               close(req->post_data_fd);
1032 +               return init_cgi(req);
1033 +               }
1034          /* if here, we can safely assume that there is more to read */
1035          req->status = BODY_READ;
1036          return 1;
1037 Index: boa-0.94.13/src/boa-plugin.h
1038 ===================================================================
1039 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
1040 +++ boa-0.94.13/src/boa-plugin.h        2008-06-29 02:02:27.000000000 +0200
1041 @@ -0,0 +1,68 @@
1042 +#ifndef _HTTPD_PLUGIN_H__
1043 +#define _HTTPD_PLUGIN_H__
1044 +
1045 +#include "list.h"
1046 +
1047 +/*
1048 + * Definition for HTTP server plugins
1049 + *
1050 + * The context that the plugin is called with for
1051 + * a single http request. It gets allocated in the
1052 + * persistent context before prepare_req and freed
1053 + * there afterwards (still active in the forked
1054 + * context at handle_req time)
1055 + */
1056 +struct http_context
1057 +{
1058 +       char *uri;
1059 +       char *request_method;
1060 +       char *server_addr;
1061 +       char *server_proto;
1062 +       char *query_string;
1063 +       char *remote_addr;
1064 +       unsigned int remote_port;
1065 +       char *content_type;
1066 +       char *content_length;
1067 +       char *http_accept;
1068 +       char *cookie;
1069 +
1070 +       void *priv;
1071 +};
1072 +
1073 +/*
1074 + * the main data structure of httpd plugins.
1075 + */
1076 +struct httpd_plugin
1077 +{
1078 +       /* used by the web server */
1079 +       struct list_head list;
1080 +
1081 +       /* only page requests matching 'prefix' are passed
1082 +        * to prepare_req and handle_req */
1083 +       const char *prefix;
1084 +
1085 +       /* directory that the plugin was found in */
1086 +       const char *dir;
1087 +
1088 +       /* initialize the plugin, if the return value is nonzero,
1089 +        * the plugin will not be used */
1090 +       int (*init)(struct httpd_plugin *);
1091 +
1092 +       /* free all memory associated with the plugin */
1093 +       void (*done)(struct httpd_plugin *);
1094 +
1095 +       /* prepare a page request. this is executed in the main context,
1096 +        * so pay attention to memory usage. should not print any data
1097 +        * to stdout */
1098 +       int (*prepare_req)(struct httpd_plugin *, struct http_context *);
1099 +
1100 +       /* handle the request. can print output data to stdout */
1101 +       int (*handle_req)(struct httpd_plugin *, struct http_context *);
1102 +
1103 +       /* pointer for private data structures of the plugin */
1104 +       void *priv;
1105 +};
1106 +
1107 +#define HTTPD_PLUGIN struct httpd_plugin httpd_plugin =
1108 +
1109 +#endif