MurataTypeYD_RPC_Sample fixed version for 050314

Dependencies:   PowerControl SNICInterface_mod2 mbed-rtos mbed

Fork of HTTPClient_WiFi_HelloWorld by KDDI Fx0 hackathon

Committer:
komoritan
Date:
Thu Mar 12 12:27:31 2015 +0000
Revision:
6:6c49fdc29825
Fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komoritan 6:6c49fdc29825 1 #include "HTTPServer.h"
komoritan 6:6c49fdc29825 2 #include "mbed.h"
komoritan 6:6c49fdc29825 3
komoritan 6:6c49fdc29825 4
komoritan 6:6c49fdc29825 5 bool cmp(char* a, char* b)
komoritan 6:6c49fdc29825 6 {
komoritan 6:6c49fdc29825 7 return strcmp(a,b) < 0;
komoritan 6:6c49fdc29825 8 }
komoritan 6:6c49fdc29825 9
komoritan 6:6c49fdc29825 10
komoritan 6:6c49fdc29825 11 HTTPServer::HTTPServer():
komoritan 6:6c49fdc29825 12 handlers(&cmp),
komoritan 6:6c49fdc29825 13 reply()
komoritan 6:6c49fdc29825 14 {
komoritan 6:6c49fdc29825 15 }
komoritan 6:6c49fdc29825 16
komoritan 6:6c49fdc29825 17
komoritan 6:6c49fdc29825 18 HTTPServer::~HTTPServer()
komoritan 6:6c49fdc29825 19 {
komoritan 6:6c49fdc29825 20 }
komoritan 6:6c49fdc29825 21
komoritan 6:6c49fdc29825 22
komoritan 6:6c49fdc29825 23 bool HTTPServer::init(int port)
komoritan 6:6c49fdc29825 24 {
komoritan 6:6c49fdc29825 25 DigitalOut led4(LED4);
komoritan 6:6c49fdc29825 26
komoritan 6:6c49fdc29825 27 socketserver.set_blocking(true);
komoritan 6:6c49fdc29825 28 if(socketserver.bind(port))
komoritan 6:6c49fdc29825 29 {
komoritan 6:6c49fdc29825 30 printf("Could not bind on port %d.\n", port);
komoritan 6:6c49fdc29825 31 return false;
komoritan 6:6c49fdc29825 32 }
komoritan 6:6c49fdc29825 33
komoritan 6:6c49fdc29825 34 if(socketserver.listen())
komoritan 6:6c49fdc29825 35 {
komoritan 6:6c49fdc29825 36 printf("Could not listen %d.\n", port);
komoritan 6:6c49fdc29825 37 return false;
komoritan 6:6c49fdc29825 38 }
komoritan 6:6c49fdc29825 39
komoritan 6:6c49fdc29825 40 led4 = 1; // server is ready
komoritan 6:6c49fdc29825 41
komoritan 6:6c49fdc29825 42 return true;
komoritan 6:6c49fdc29825 43 }
komoritan 6:6c49fdc29825 44
komoritan 6:6c49fdc29825 45
komoritan 6:6c49fdc29825 46 void HTTPServer::run()
komoritan 6:6c49fdc29825 47 {
komoritan 6:6c49fdc29825 48 char buffer[1024];
komoritan 6:6c49fdc29825 49 TCPSocketConnection c;
komoritan 6:6c49fdc29825 50
komoritan 6:6c49fdc29825 51 while(true)
komoritan 6:6c49fdc29825 52 {
komoritan 6:6c49fdc29825 53 // KTEC MOD START
komoritan 6:6c49fdc29825 54 // SNICInterface_modとNySNICInterfaceのIF差分が存在するため、変更
komoritan 6:6c49fdc29825 55 //while(socketserver.accept(&c));
komoritan 6:6c49fdc29825 56 while(socketserver.accept(c));
komoritan 6:6c49fdc29825 57 // KTEC MOD END
komoritan 6:6c49fdc29825 58 c.set_blocking(false, 1000);
komoritan 6:6c49fdc29825 59
komoritan 6:6c49fdc29825 60 while(c.is_connected())
komoritan 6:6c49fdc29825 61 {
komoritan 6:6c49fdc29825 62 int n = c.receive(buffer, sizeof(buffer)-1);
komoritan 6:6c49fdc29825 63 if(n == 0)
komoritan 6:6c49fdc29825 64 {
komoritan 6:6c49fdc29825 65 c.close();
komoritan 6:6c49fdc29825 66 break;
komoritan 6:6c49fdc29825 67 }
komoritan 6:6c49fdc29825 68 else if(n != -1)
komoritan 6:6c49fdc29825 69 {
komoritan 6:6c49fdc29825 70 buffer[n] = '\0';
komoritan 6:6c49fdc29825 71 printf("Received data -- %s --. \r\n", buffer);
komoritan 6:6c49fdc29825 72 handle_request(buffer);
komoritan 6:6c49fdc29825 73 create_response(buffer);
komoritan 6:6c49fdc29825 74 printf("Sending data -- %s --. \r\n", buffer);
komoritan 6:6c49fdc29825 75 c.send_all(buffer, strlen(buffer));
komoritan 6:6c49fdc29825 76 printf("done. \r\n");
komoritan 6:6c49fdc29825 77 c.close();
komoritan 6:6c49fdc29825 78 break;
komoritan 6:6c49fdc29825 79 }
komoritan 6:6c49fdc29825 80 else {
komoritan 6:6c49fdc29825 81 printf("Error while receiving data. \r\n");
komoritan 6:6c49fdc29825 82 c.close();
komoritan 6:6c49fdc29825 83 break;
komoritan 6:6c49fdc29825 84 }
komoritan 6:6c49fdc29825 85 }
komoritan 6:6c49fdc29825 86 }
komoritan 6:6c49fdc29825 87 }
komoritan 6:6c49fdc29825 88
komoritan 6:6c49fdc29825 89
komoritan 6:6c49fdc29825 90 void HTTPServer::handle_request(char *buffer)
komoritan 6:6c49fdc29825 91 {
komoritan 6:6c49fdc29825 92 char* request_type = strtok(buffer, " ");
komoritan 6:6c49fdc29825 93 char* request = strtok(NULL, " ");
komoritan 6:6c49fdc29825 94
komoritan 6:6c49fdc29825 95 reply[0] = '\0';
komoritan 6:6c49fdc29825 96 response_code = HTTP_404_NOTFOUND;
komoritan 6:6c49fdc29825 97
komoritan 6:6c49fdc29825 98 if(!object.decode(request, reply)){
komoritan 6:6c49fdc29825 99 // KTEC MOD START
komoritan 6:6c49fdc29825 100 // レスポンスコードを設定
komoritan 6:6c49fdc29825 101 response_code = HTTP_200_OK;
komoritan 6:6c49fdc29825 102 return;
komoritan 6:6c49fdc29825 103 } else {
komoritan 6:6c49fdc29825 104 response_code = HTTP_400_BADREQUEST;
komoritan 6:6c49fdc29825 105 return;
komoritan 6:6c49fdc29825 106 }
komoritan 6:6c49fdc29825 107 // KTEC MOD END
komoritan 6:6c49fdc29825 108
komoritan 6:6c49fdc29825 109 // KTEC DEL START
komoritan 6:6c49fdc29825 110 /*
komoritan 6:6c49fdc29825 111 std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type);
komoritan 6:6c49fdc29825 112 if(itor == handlers.end())
komoritan 6:6c49fdc29825 113 {
komoritan 6:6c49fdc29825 114 printf("No request handler found for this type of request.\r\n");
komoritan 6:6c49fdc29825 115 return;
komoritan 6:6c49fdc29825 116 }
komoritan 6:6c49fdc29825 117 if(itor->second != NULL)
komoritan 6:6c49fdc29825 118 response_code = itor->second->handle(object, reply);
komoritan 6:6c49fdc29825 119 else
komoritan 6:6c49fdc29825 120 printf("Invalid request handler\r\n");
komoritan 6:6c49fdc29825 121 */
komoritan 6:6c49fdc29825 122 // KTEC DEL END
komoritan 6:6c49fdc29825 123 }
komoritan 6:6c49fdc29825 124
komoritan 6:6c49fdc29825 125 void HTTPServer::create_response(char *buffer)
komoritan 6:6c49fdc29825 126 {
komoritan 6:6c49fdc29825 127 char content_length[30] = "";
komoritan 6:6c49fdc29825 128 buffer[0] = '\0';
komoritan 6:6c49fdc29825 129
komoritan 6:6c49fdc29825 130 /* HTTP Status Code */
komoritan 6:6c49fdc29825 131 strcat(buffer, "HTTP/1.1 ");
komoritan 6:6c49fdc29825 132 switch(response_code){
komoritan 6:6c49fdc29825 133 case HTTP_200_OK:
komoritan 6:6c49fdc29825 134 strcat(buffer, "200 OK\r\n");
komoritan 6:6c49fdc29825 135 break;
komoritan 6:6c49fdc29825 136 case HTTP_404_NOTFOUND:
komoritan 6:6c49fdc29825 137 strcat(buffer, "404 Not Found\r\n");
komoritan 6:6c49fdc29825 138 break;
komoritan 6:6c49fdc29825 139 default:
komoritan 6:6c49fdc29825 140 strcat(buffer, "500 Internal Server Error\r\n");
komoritan 6:6c49fdc29825 141 break;
komoritan 6:6c49fdc29825 142 }
komoritan 6:6c49fdc29825 143
komoritan 6:6c49fdc29825 144 /* add header */
komoritan 6:6c49fdc29825 145 strcat(buffer, "Access-Control-Allow-Origin: *\r\n");
komoritan 6:6c49fdc29825 146 sprintf(content_length, "Content-Length: %d\r\n", strlen(reply));
komoritan 6:6c49fdc29825 147 strncat(buffer, content_length, strlen(content_length));
komoritan 6:6c49fdc29825 148 strcat(buffer, "Content-Type: text/plain\r\n\r\n");
komoritan 6:6c49fdc29825 149
komoritan 6:6c49fdc29825 150 /* add content */
komoritan 6:6c49fdc29825 151 strcat(buffer, reply);
komoritan 6:6c49fdc29825 152 }
komoritan 6:6c49fdc29825 153
komoritan 6:6c49fdc29825 154
komoritan 6:6c49fdc29825 155 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
komoritan 6:6c49fdc29825 156 {
komoritan 6:6c49fdc29825 157 handlers[name] = handler;
komoritan 6:6c49fdc29825 158 printf("%s request hander.\r\n", name);
komoritan 6:6c49fdc29825 159 }