Commit 4b5bcf89c0cc1ba8f06cbae39a266bdd8bb29dac

Authored by Georg Hopp
1 parent 1fdcbd40

a taste of ajax

... ... @@ -43,7 +43,8 @@ HttpResponse httpResponse304(
43 43 const char *, size_t,
44 44 const char *, size_t);
45 45 HttpResponse httpResponse404();
46   -HttpResponse httpResponseMe(int);
  46 +HttpResponse httpResponseMe();
  47 +HttpResponse httpResponseRandval(int);
47 48 HttpResponse httpResponseAsset(
48 49 const char *,
49 50 const char *, size_t,
... ...
... ... @@ -27,7 +27,8 @@ RESP = http/response.c \
27 27 http/response/304.c \
28 28 http/response/404.c \
29 29 http/response/asset.c \
30   - http/response/me.c
  30 + http/response/me.c \
  31 + http/response/randval.c
31 32 PARSER = http/parser.c \
32 33 http/parser/parse.c \
33 34 http/parser/new_message.c \
... ...
... ... @@ -39,27 +39,47 @@
39 39 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" \
40 40 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">" \
41 41 "<head>" \
42   - "<title>200 - OK</title>" \
  42 + "<title>My own little Web-App</title>" \
  43 + "<style type=\"text/css\">" \
  44 + "div#randval {" \
  45 + "width: 120px;" \
  46 + "height: 30px;" \
  47 + "left: 200px;" \
  48 + "top: 100px;" \
  49 + "position: fixed;" \
  50 + "overflow: none;" \
  51 + "background-color: white;" \
  52 + "border: 1px solid black;" \
  53 + "}" \
  54 + "div.hide#randval {" \
  55 + "top: -50px;" \
  56 + "}" \
  57 + "</style>" \
43 58 "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \
44 59 "<script>" \
45 60 "$(document).ready(function() {" \
46 61 "$(\"a\").click(function() {" \
47   - "alert(\"Hello world!\");" \
  62 + "$(\"#randval\").load(\"/randval/\");" \
  63 + "$(\"#randval\").removeClass(\"hide\");" \
  64 + "});" \
  65 + "$(\"#randval\").click(function() {" \
  66 + "$(\"#randval\").addClass(\"hide\");" \
48 67 "});" \
49 68 "});" \
50 69 "</script>" \
51 70 "</head>" \
52 71 "<body>" \
53   - "<h1>Testpage</h1>" \
54   - "<img src=\"/image/\" />" \
55   - "<hr />%02d" \
56   - "<hr /><a href=\"#\">Link</a>" \
  72 + "<div id=\"randval\" class=\"hide\"></div>" \
  73 + "<div id=\"main\">" \
  74 + "<h1>Testpage</h1>" \
  75 + "<img src=\"/image/\" />" \
  76 + "<hr /><a href=\"#\">Link</a>" \
  77 + "</div>" \
57 78 "</body>" \
58 79 "</html>"
59 80
60   -
61 81 HttpResponse
62   -httpResponseMe(int value)
  82 +httpResponseMe()
63 83 {
64 84 char buffer[200];
65 85 HttpResponse response;
... ... @@ -89,9 +109,9 @@ httpResponseMe(int value)
89 109 sizeof("profession=\"coder\"")-1));
90 110
91 111 message->type = HTTP_MESSAGE_BUFFERED;
92   - message->nbody = sizeof(RESP_DATA)-1-2;
93   - message->body = malloc(sizeof(RESP_DATA)-2);
94   - sprintf(message->body, RESP_DATA, value);
  112 + message->nbody = sizeof(RESP_DATA)-1;
  113 + message->body = malloc(sizeof(RESP_DATA)-1);
  114 + memcpy(message->body, RESP_DATA, sizeof(RESP_DATA)-1);
95 115
96 116 nbuf = sprintf(buffer, "%d", message->nbody);
97 117
... ...
... ... @@ -55,7 +55,11 @@ httpWorkerProcess(HttpWorker this, int fd)
55 55 if (0 == strcmp("GET", request->method)) {
56 56
57 57 if (0 == strcmp("/me/", request->uri)) {
58   - response = (HttpMessage)httpResponseMe(*(this->val));
  58 + response = (HttpMessage)httpResponseMe();
  59 + }
  60 +
  61 + if (0 == strcmp("/randval/", request->uri)) {
  62 + response = (HttpMessage)httpResponseRandval(*(this->val));
59 63 }
60 64
61 65 if (0 == strcmp("/image/", request->uri)) {
... ...
... ... @@ -24,7 +24,7 @@
24 24 #include "interface/logger.h"
25 25 #include "interface/stream_writer.h"
26 26
27   -void serverCloseConn(Server, unsigned int);
  27 +void serverCloseConn(Server, unsigned int);
28 28
29 29 ssize_t
30 30 serverWrite(Server this, unsigned int i)
... ... @@ -36,7 +36,7 @@ serverWrite(Server this, unsigned int i)
36 36 loggerLog(
37 37 this->logger,
38 38 LOGGER_INFO,
39   - "initialization error: NULL reader");
  39 + "initialization error: NULL worker");
40 40 return -1;
41 41 }
42 42
... ...
Please register or login to post a comment