1 #include <HttpServer.h> 2 #include <HttpServerLib.h> 3 #include <HttpServCon.h> 4 #include <time.h> 5 #include <smx.h> 6 #include <pcutil.h> 7 8 9 typedef struct 10 { 11 HttpPage super; /* As if inherited */ 12 } SetGetTimePage; 13 14 15 void SetGetTimePage_constructor(SetGetTimePage* o); 16 void SetGetTimePage_service(HttpPage* o, 17 HttpRequest* request, 18 HttpResponse* response); 19 void SetGetTimePage_doGet(SetGetTimePage* o, HttpResponse* response); 20 void SetGetTimePage_doPost(SetGetTimePage* o, 21 HttpRequest* request, 22 HttpResponse* response); 23 void SetGetTimePage_setRTC(SetGetTimePage* o, struct tm* tm); 24 25 26 27 28 void 29 SetGetTimePage_constructor(SetGetTimePage* o) 30 { 31 HttpPage_constructor((HttpPage*)o, 32 SetGetTimePage_service, 33 "index.html"); 34 } 35 36 void 37 SetGetTimePage_service(HttpPage* o, 38 HttpRequest* request, 39 HttpResponse* response) 40 { 41 42 HttpResponse_write(response, 43 "<html><head>" 44 "<title>Set or get SMX time</title>" 45 "</head><body>", 46 -1, TRUE); 47 48 if(HttpRequest_getMethodType(request) == HttpRequest_Post) 49 SetGetTimePage_doPost((SetGetTimePage*)o,request,response); 50 else 51 SetGetTimePage_doGet((SetGetTimePage*)o,response); 52 53 HttpResponse_write(response,"</body></html>", -1, TRUE); 54 } 55 56 57 void 58 SetGetTimePage_doGet(SetGetTimePage* o, HttpResponse* response) 59 { 60 static const char* wd[7] = { 61 "Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat" 62 }; 63 static const char* months[12] = { 64 "Jan","Feb", "Mar", "Apr", "May", "Jun", 65 "Jul","Aug", "Sep", "Oct", "Nov", "Dec" 66 }; 67 struct tm tm; 68 U32 time = get_stime(); 69 httpTime2tm(&tm, time); 70 HttpResponse_write(response,"<p>Current day and time is: ",-1,TRUE); 71 HttpResponse_printf(response, 72 "%s, %02d %s %d %02d:%02d:%02d GMT", 73 wd[tm.tm_wday], 74 tm.tm_mday, 75 months[tm.tm_mon], 76 tm.tm_year+1900, 77 tm.tm_hour, 78 tm.tm_min, 79 tm.tm_sec); 80 HttpResponse_write(response,"</p>", -1, TRUE); 81 HttpResponse_write(response, 82 "<p>" 83 "<form method='post'>" 84 "<input type='text' name='time' size='30'/>" 85 "<br/>" 86 "<input type='submit'/>" 87 "</form>" 88 "</p>", 89 -1, TRUE); 90 } 91 92 93 void 94 SetGetTimePage_doPost(SetGetTimePage* o, 95 HttpRequest* request, 96 HttpResponse* response) 97 { 98 HttpTime time; 99 struct tm tm; 100 const char* timeStr = HttpRequest_getParameter(request,"time"); 101 if(timeStr == NULL || (time=httpParseDate(timeStr)) == 0) 102 { 103 HttpResponse_printf( 104 response, 105 "<p>" 106 "Cannot parse date string. " 107 "Date string must be in RFC1123 or RFC850 format.<br/>" 108 "<a href='%s'>Try again</a>" 109 "</p>", 110 HttpPage_getName((HttpPage*)o)); 111 } 112 else 113 { 114 httpTime2tm(&tm, time); 115 setRTC(o, &tm); 116 HttpResponse_sendRedirect( 117 response, 118 HttpResponse_encodeRedirectURL( 119 response, 120 HttpPage_getName((HttpPage*)o))); 121 } 122 } 123 124 125 void 126 SetGetTimePage_setRTC(SetGetTimePage* o, struct tm* tm) 127 { 128 #if PC_RTC 129 Set_RTC_Time(tm); 130 Set_RTC_Date(tm); 131 #endif 132 } 133 134 135 136 void 137 barracudaMain() 138 { 139 140 HttpDispatcher dispatcher; 141 HttpServer server; 142 HttpServCon serverCon; 143 HttpDir rootDir; 144 SetGetTimePage setGetTimePage; 145 int port=8080; 146 147 #ifdef WIN32 148 WORD wVersionRequested; 149 WSADATA wsaData; 150 wVersionRequested = MAKEWORD(1, 1); 151 if(WSAStartup( wVersionRequested, &wsaData )) 152 perror("WSAStartup"); 153 #endif 154 155 156 /*Create the socket dispatcher object.*/ 157 HttpDispatcher_constructor(&dispatcher); 158 /*Create the Web-Server*/ 159 HttpServer_constructor(&server, &dispatcher); 160 /*Create a server socket listener that listen on all interfaces.*/ 161 HttpServCon_constructor(&serverCon, &server, port, AF_INET, 0, 0); 162 163 if( !HttpServCon_isValid(&serverCon) ) 164 { /*A primitive error handling*/ 165 assert(0); 166 return; 167 } 168 169 /*Root dir takes no parameters i.e. no name*/ 170 HttpDir_constructor(&rootDir,0, 0); 171 SetGetTimePage_constructor, &setGetTimePage); 172 HttpDir_insertPage(&rootDir, (HttpPage*)&se&setGetTimePage); 173 HttpServer_insertRootDir(&server, &rootDir); 174 175 #ifdef WIN32 176 for(;;) 177 { 178 struct timeval t; 179 t.tv_sec=0; 180 t.tv_usec=0; 181 HttpDispatcher_run(&dispatcher, &t); 182 Thread_sleep(50); /*50 mSec. */ 183 } 184 #else 185 HttpDispatcher_run(&dispatcher, 0);/*Never returns*/ 186 #endif 187 } 188 189 190 void 191 startBarracuda(void) 192 { 193 startx(create_task(barracudaMain, ct->priority, 8000)); 194 }