r17098 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17097‎ | r17098 | r17099 >
Date:01:12, 19 October 2006
Author:river
Status:old
Tags:
Comment:
C++ify header_list + optimisations
Modified paths:
  • /trunk/willow/src/include/whttp_entity.h (modified) (history)
  • /trunk/willow/src/willow/whttp.cc (modified) (history)
  • /trunk/willow/src/willow/whttp_entity.cc (modified) (history)

Diff [purge]

Index: trunk/willow/src/willow/whttp_entity.cc
@@ -62,6 +62,9 @@
6363 #include <ctype.h>
6464 #include <zlib.h>
6565
 66+#include <vector>
 67+using std::vector;
 68+
6669 #include "willow.h"
6770 #include "whttp.h"
6871 #include "whttp_entity.h"
@@ -175,6 +178,7 @@
176179 entity_send(fde *fde, http_entity *entity, header_cb cb, void *data, int flags)
177180 {
178181 struct header_list *hl;
 182+vector<header *>::iterator vit, vend;
179183 int window = 15;
180184
181185 errno = 0;
@@ -212,7 +216,7 @@
213217 }
214218
215219 if (flags & ENT_CHUNKED_OKAY) {
216 - struct header_list *contlen;
 220+ struct header *contlen;
217221 entity->he_flags.chunked = 1;
218222 if (!entity->he_h_transfer_encoding)
219223 header_add(&entity->he_headers, wstrdup("Transfer-Encoding"), wstrdup("chunked"));
@@ -238,8 +242,9 @@
239243 }
240244 header_add(&entity->he_headers, wstrdup("Content-Encoding"),
241245 wstrdup(ent_encodings[entity->he_encoding]));
242 - for (hl = entity->he_headers.hl_next; hl; hl = hl->hl_next)
243 - evbuffer_add_printf(entity->_he_tobuf->output, "%s: %s\r\n", hl->hl_name, hl->hl_value);
 246+ for (vit = entity->he_headers.hl_hdrs.begin(), vend = entity->he_headers.hl_hdrs.end();
 247+ vit != vend; ++vit)
 248+ evbuffer_add_printf(entity->_he_tobuf->output, "%s: %s\r\n", (*vit)->hr_name, (*vit)->hr_value);
244249 bufferevent_write(entity->_he_tobuf, const_cast<char *>("\r\n"), 2);
245250 }
246251
@@ -739,17 +744,14 @@
740745 void
741746 header_free(header_list *head)
742747 {
743 -struct header_list *next = head->hl_next;
744 -
745 - while (next) {
746 - struct header_list *here = next;
747 - next = here->hl_next;
748 - wfree((char *)here->hl_name);
749 - wfree((char *)here->hl_value);
750 - wfree(here);
 748+vector<header *>::iterator vit, vend;
 749+ for (vit = head->hl_hdrs.begin(), vend = head->hl_hdrs.end(); vit != vend; ++vit) {
 750+ wfree((*vit)->hr_name);
 751+ wfree((*vit)->hr_value);
 752+ delete *vit;
751753 }
752 -
753 - bzero(head, sizeof(*head));
 754+ head->hl_hdrs.clear();
 755+ head->hl_len = 0;
754756 }
755757
756758 #ifdef __lint
@@ -759,67 +761,42 @@
760762 void
761763 header_add(header_list *head, char *name, char *value)
762764 {
763 -struct header_list *n = head;
764 -
765 - head->hl_num++;
766 -
767 - if (head->hl_tail)
768 - n = head->hl_tail;
769 - else
770 - while (n->hl_next)
771 - n = n->hl_next;
772 - n->hl_next = (header_list *)wmalloc(sizeof(*head->hl_next));
773 - head->hl_tail = n->hl_next;
774 - head->hl_len += strlen(name) + strlen(value) + 4;
775 - n = n->hl_next;
776 - n->hl_name = name;
777 - n->hl_value = value;
778 - n->hl_next = n->hl_tail = NULL;
779 - n->hl_flags = 0;
 765+ head->append(new header(name, value));
780766 }
781767
782768 void
783769 header_append_last(header_list *head, const char *append)
784770 {
785 -struct header_list *last = head;
786 - char *cur;
787 -
788 - assert(last->hl_next);
789 -
790 - while (last->hl_next)
791 - last = last->hl_next;
792 -
793 - cur = last->hl_value;
794 - last->hl_value = (char *)wmalloc(strlen(cur) + 1 + strlen(append) + 1);
795 - sprintf(last->hl_value, "%s %s", cur, append);
796 - wfree(cur);
 771+header *last;
 772+char *tmp;
 773+ last = *head->hl_hdrs.rbegin();
 774+ tmp = last->hr_value;
 775+ last->hr_value = (char *)wmalloc(strlen(tmp) + strlen(append) + 2);
 776+ sprintf(last->hr_value, "%s %s", tmp, append);
 777+ wfree(tmp);
797778 }
798 -
799779
800780 void
801 -header_remove(header_list *head, header_list *it)
 781+header_remove(header_list *head, header *it)
802782 {
803 -struct header_list *jt;
 783+vector<header *>::iterator vit;
 784+ if ((vit = std::find(head->hl_hdrs.begin(), head->hl_hdrs.end(), it)) == head->hl_hdrs.end())
 785+ return;
 786+ wfree((*vit)->hr_name);
 787+ wfree((*vit)->hr_value);
 788+ delete *vit;
804789
805 - jt = head;
806 - while (jt->hl_next && jt->hl_next != it)
807 - jt = jt->hl_next;
808 - jt->hl_next = jt->hl_next->hl_next;
809 - if (it == head->hl_tail)
810 - head->hl_tail = jt;
811 - wfree(it->hl_name);
812 - wfree(it->hl_value);
813 - wfree(it);
 790+ std::swap(*vit, *head->hl_hdrs.rbegin());
 791+ head->hl_hdrs.pop_back();
814792 }
815793
816 -struct header_list *
 794+struct header *
817795 header_find(header_list *head, const char *name)
818796 {
819 -struct header_list *it;
820 -
821 - for (it = head->hl_next; it; it = it->hl_next)
822 - if (!strcasecmp(name, it->hl_name))
823 - return it;
 797+vector<header *>::iterator vit, vend;
 798+ for (vit = head->hl_hdrs.begin(), vend = head->hl_hdrs.end(); vit != vend; ++vit)
 799+ if (!strcasecmp(name, (*vit)->hr_name))
 800+ return *vit;
824801 return NULL;
825802 }
826803
@@ -829,19 +806,18 @@
830807 char *
831808 header_build(header_list *head)
832809 {
833 - char *buf;
834 - size_t bufsz;
835 - size_t buflen = 0;
 810+char *buf;
 811+size_t bufsz;
 812+size_t buflen = 0;
 813+vector<header *>::iterator vit, vend;
836814
837815 bufsz = head->hl_len + 3;
838816 if ((buf = (char *)wmalloc(bufsz)) == NULL)
839817 outofmemory();
840818
841819 *buf = '\0';
842 - while (head->hl_next) {
843 - head = head->hl_next;
844 - buflen += snprintf(buf + buflen, bufsz - buflen - 1, "%s: %s\r\n", head->hl_name, head->hl_value);
845 - }
 820+ for (vit = head->hl_hdrs.begin(), vend = head->hl_hdrs.end(); vit != vend; ++vit)
 821+ buflen += snprintf(buf + buflen, bufsz - buflen - 1, "%s: %s\r\n", (*vit)->hr_name, (*vit)->hr_value);
846822 if (strlcat(buf, "\r\n", bufsz) >= bufsz)
847823 abort();
848824
@@ -854,26 +830,19 @@
855831 void
856832 header_dump(header_list *head, int fd)
857833 {
858 - int i = 0;
859 -struct header_list *h;
860 -
861 - h = head->hl_next;
862 - while (h) {
863 - h = h->hl_next;
864 - ++i;
865 - }
866 -
 834+vector<header *>::iterator vit, vend;
 835+int i = 0;
 836+ i = head->hl_hdrs.size();
867837 write(fd, &i, sizeof(i));
868 -
869 - while (head->hl_next) {
 838+
 839+ for (vit = head->hl_hdrs.begin(), vend = head->hl_hdrs.end(); vit != vend; ++vit) {
870840 int j, k;
871 - head = head->hl_next;
872 - k = strlen(head->hl_name);
 841+ k = strlen((*vit)->hr_name);
873842 write(fd, &k, sizeof(k));
874 - j = strlen(head->hl_value);
 843+ j = strlen((*vit)->hr_value);
875844 write(fd, &j, sizeof(j));
876 - write(fd, head->hl_name, k);
877 - write(fd, head->hl_value, j);
 845+ write(fd, (*vit)->hr_name, k);
 846+ write(fd, (*vit)->hr_value, j);
878847 }
879848 }
880849
@@ -881,11 +850,10 @@
882851 header_undump(header_list *head, int fd, off_t *len)
883852 {
884853 int i = 0, j = 0, sz = 0;
885 -struct header_list *it = head;
886854 ssize_t r;
887855
888856 *len = 0;
889 - bzero(head, sizeof(*head));
 857+ head->hl_hdrs.clear();
890858 if ((r = read(fd, &sz, sizeof(sz))) < 0) {
891859 wlog(WLOG_WARNING, "reading cache file: %s", strerror(errno));
892860 return -1; /* XXX */
@@ -895,12 +863,9 @@
896864 WDEBUG((WLOG_DEBUG, "header_undump: %d entries", sz));
897865
898866 while (sz--) {
899 - char *n, *v, *s;
900 - int k;
901 -
902 - if ((it->hl_next = (header_list *)wcalloc(1, sizeof(struct header_list))) == NULL)
903 - outofmemory();
904 - it = it->hl_next;
 867+ char *n, *v, *s;
 868+ int k;
 869+ header *h;
905870 *len += read(fd, &i, sizeof(i));
906871 *len += read(fd, &j, sizeof(j));
907872 WDEBUG((WLOG_DEBUG, "header_undump: i=%d j=%d", i, j));
@@ -914,12 +879,10 @@
915880 *len += k;
916881 s += k;
917882 *s = '\0';
918 - it->hl_name = n;
919 - it->hl_value = wstrdup(v);
920 - head->hl_len += i + j + 4;
 883+ h = new header(n, wstrdup(v));
 884+ head->append(h);
921885 }
922886
923 - head->hl_tail = it;
924887 return 0;
925888 }
926889
@@ -959,7 +922,7 @@
960923 case ENTITY_STATE_HDR:
961924 if (isspace(*line)) {
962925 char *s = line;
963 - if (!entity->he_headers.hl_next) {
 926+ if (!entity->he_headers.hl_hdrs.size()) {
964927 error = ENT_ERR_INVHDR;
965928 goto error;
966929 }
@@ -985,7 +948,7 @@
986949 WDEBUG((WLOG_DEBUG, "header: from [%s], [%s] = [%s]",
987950 line, hdr[0], value));
988951
989 - if (++entity->he_headers.hl_num > MAX_HEADERS) {
 952+ if (entity->he_headers.hl_hdrs.size() > MAX_HEADERS) {
990953 error = ENT_ERR_2MANY;
991954 goto error;
992955 }
Index: trunk/willow/src/willow/whttp.cc
@@ -371,7 +371,8 @@
372372 proxy_start_backend(backend *backend, fde *e, void *data)
373373 {
374374 struct http_client *client = (http_client *)data;
375 -struct header_list *it;
 375+struct header *hdr;
 376+vector<header *>::iterator it, end;
376377 int error = 0;
377378 socklen_t len = sizeof(error);
378379
@@ -394,10 +395,11 @@
395396 return;
396397 }
397398
398 - for (it = client->cl_entity.he_headers.hl_next; it; it = it->hl_next) {
399 - if (removable_header(it->hl_name)) {
400 - header_remove(&client->cl_entity.he_headers, it);
401 - it = client->cl_entity.he_headers.hl_next;
 399+ for (it = client->cl_entity.he_headers.hl_hdrs.begin(),
 400+ end = client->cl_entity.he_headers.hl_hdrs.end();
 401+ it != end; ++it) {
 402+ if (removable_header((*it)->hr_name)) {
 403+ header_remove(&client->cl_entity.he_headers, *it);
402404 continue;
403405 }
404406 }
@@ -585,8 +587,8 @@
586588 }
587589
588590 if (client->cl_co) {
589 - int complete = (res != -1);
590 - struct header_list *hdr;
 591+ int complete = (res != -1);
 592+ struct header *hdr;
591593
592594 /*
593595 * The server may have indicated that we shouldn't cache this document.
@@ -598,7 +600,7 @@
599601 */
600602 hdr = header_find(&client->cl_entity.he_headers, "Pragma");
601603 if (hdr) {
602 - char **pragmas = wstrvec(hdr->hl_value, ",", 0);
 604+ char **pragmas = wstrvec(hdr->hr_value, ",", 0);
603605 char **s;
604606 for (s = pragmas; *s; ++s) {
605607 if (!strcasecmp(*s, "no-cache")) {
@@ -614,7 +616,7 @@
615617 */
616618 hdr = header_find(&client->cl_entity.he_headers, "Cache-Control");
617619 if (hdr) {
618 - char **controls = wstrvec(hdr->hl_value, ",", 0);
 620+ char **controls = wstrvec(hdr->hr_value, ",", 0);
619621 char **s;
620622 for (s = controls; *s; ++s) {
621623 /*
Index: trunk/willow/src/include/whttp_entity.h
@@ -55,14 +55,23 @@
5656 typedef void (*header_cb)(struct http_entity *, void *, int);
5757 typedef void (*cache_callback)(const char *, size_t, void *);
5858
 59+struct header {
 60+ header(char *n, char *v)
 61+ : hr_name(n), hr_value(v) {}
 62+ char *hr_name;
 63+ char *hr_value;
 64+};
 65+
5966 struct header_list {
60 - char *hl_name;
61 - char *hl_value;
62 -struct header_list *hl_next;
63 -struct header_list *hl_tail;
 67+ header_list() : hl_len(0) {
 68+ hl_hdrs.reserve(20); /* should be enough for most requests */
 69+ }
 70+ void append(header *h) {
 71+ hl_hdrs.push_back(h);
 72+ hl_len += strlen(h->hr_name) + strlen(h->hr_value) + 4;
 73+ }
 74+ vector<header *> hl_hdrs;
6475 int hl_len;
65 - int hl_num;
66 - int hl_flags;
6776 };
6877
6978 struct qvalue {
@@ -180,10 +189,10 @@
181190 void header_append_last (struct header_list *, const char *);
182191 void header_free (struct header_list *);
183192 char *header_build (struct header_list *);
184 - void header_remove (struct header_list *, struct header_list *);
 193+ void header_remove (struct header_list *, struct header *);
185194 void header_dump (struct header_list *, int);
186195 int header_undump (struct header_list *, int, off_t *);
187 -struct header_list * header_find(struct header_list *head, const char *name);
 196+struct header *header_find (struct header_list *head, const char *name);
188197
189198
190199 #endif