![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
more
Dependencies: C12832_lcd Http_Wifi_Fileserver ZacsProgram mbed
Fork of HTTPServerHelloWorld by
PostHandler.cpp@3:c16d9e9076a1, 2014-10-29 (annotated)
- Committer:
- wellmon7
- Date:
- Wed Oct 29 01:42:04 2014 +0000
- Revision:
- 3:c16d9e9076a1
Trial 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wellmon7 | 3:c16d9e9076a1 | 1 | /* |
wellmon7 | 3:c16d9e9076a1 | 2 | SY202 Final Project |
wellmon7 | 3:c16d9e9076a1 | 3 | 04 MAR 2014 |
wellmon7 | 3:c16d9e9076a1 | 4 | |
wellmon7 | 3:c16d9e9076a1 | 5 | Created by: |
wellmon7 | 3:c16d9e9076a1 | 6 | MIDN 3/C Villemez |
wellmon7 | 3:c16d9e9076a1 | 7 | MIDN 3/C DaCruz |
wellmon7 | 3:c16d9e9076a1 | 8 | MIDN 3/C Wellmon |
wellmon7 | 3:c16d9e9076a1 | 9 | |
wellmon7 | 3:c16d9e9076a1 | 10 | Resources: |
wellmon7 | 3:c16d9e9076a1 | 11 | LCDR Hoffmeister |
wellmon7 | 3:c16d9e9076a1 | 12 | MBED Cookbook |
wellmon7 | 3:c16d9e9076a1 | 13 | |
wellmon7 | 3:c16d9e9076a1 | 14 | */ |
wellmon7 | 3:c16d9e9076a1 | 15 | |
wellmon7 | 3:c16d9e9076a1 | 16 | #include "PostHandler.h" |
wellmon7 | 3:c16d9e9076a1 | 17 | #include "string.h" |
wellmon7 | 3:c16d9e9076a1 | 18 | #include <stdio.h> |
wellmon7 | 3:c16d9e9076a1 | 19 | #include <string.h> |
wellmon7 | 3:c16d9e9076a1 | 20 | #include <ctype.h> |
wellmon7 | 3:c16d9e9076a1 | 21 | #include "C12832_lcd.h" |
wellmon7 | 3:c16d9e9076a1 | 22 | #include <stdlib.h> |
wellmon7 | 3:c16d9e9076a1 | 23 | |
wellmon7 | 3:c16d9e9076a1 | 24 | |
wellmon7 | 3:c16d9e9076a1 | 25 | char* myData; |
wellmon7 | 3:c16d9e9076a1 | 26 | |
wellmon7 | 3:c16d9e9076a1 | 27 | PostHandler::PostHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket):HTTPRequestHandler(rootPath, path, pTCPSocket){ |
wellmon7 | 3:c16d9e9076a1 | 28 | } |
wellmon7 | 3:c16d9e9076a1 | 29 | |
wellmon7 | 3:c16d9e9076a1 | 30 | |
wellmon7 | 3:c16d9e9076a1 | 31 | PostHandler::~PostHandler(){ |
wellmon7 | 3:c16d9e9076a1 | 32 | } |
wellmon7 | 3:c16d9e9076a1 | 33 | |
wellmon7 | 3:c16d9e9076a1 | 34 | void PostHandler::doGet(){ |
wellmon7 | 3:c16d9e9076a1 | 35 | } |
wellmon7 | 3:c16d9e9076a1 | 36 | |
wellmon7 | 3:c16d9e9076a1 | 37 | void PostHandler::doPost() |
wellmon7 | 3:c16d9e9076a1 | 38 | { |
wellmon7 | 3:c16d9e9076a1 | 39 | |
wellmon7 | 3:c16d9e9076a1 | 40 | //get post variables |
wellmon7 | 3:c16d9e9076a1 | 41 | string _path = path(); |
wellmon7 | 3:c16d9e9076a1 | 42 | string _rootpath = rootPath(); |
wellmon7 | 3:c16d9e9076a1 | 43 | int _datalen = dataLen(); |
wellmon7 | 3:c16d9e9076a1 | 44 | |
wellmon7 | 3:c16d9e9076a1 | 45 | } |
wellmon7 | 3:c16d9e9076a1 | 46 | |
wellmon7 | 3:c16d9e9076a1 | 47 | void PostHandler::doHead(){ |
wellmon7 | 3:c16d9e9076a1 | 48 | } |
wellmon7 | 3:c16d9e9076a1 | 49 | |
wellmon7 | 3:c16d9e9076a1 | 50 | void PostHandler::onReadable(){ |
wellmon7 | 3:c16d9e9076a1 | 51 | |
wellmon7 | 3:c16d9e9076a1 | 52 | C12832_LCD lcd; |
wellmon7 | 3:c16d9e9076a1 | 53 | Serial pcx(USBTX, USBRX); |
wellmon7 | 3:c16d9e9076a1 | 54 | |
wellmon7 | 3:c16d9e9076a1 | 55 | int _datalen = dataLen(); //read POST data length |
wellmon7 | 3:c16d9e9076a1 | 56 | if(_datalen <= 1024){ //makes sure that submitted html data is less than size of the buffer |
wellmon7 | 3:c16d9e9076a1 | 57 | char _read_data[1024] = {}; |
wellmon7 | 3:c16d9e9076a1 | 58 | |
wellmon7 | 3:c16d9e9076a1 | 59 | //read POST data |
wellmon7 | 3:c16d9e9076a1 | 60 | readData(_read_data, _datalen); |
wellmon7 | 3:c16d9e9076a1 | 61 | myData = _read_data; |
wellmon7 | 3:c16d9e9076a1 | 62 | |
wellmon7 | 3:c16d9e9076a1 | 63 | pcx.printf("TYPED: %s", myData); |
wellmon7 | 3:c16d9e9076a1 | 64 | |
wellmon7 | 3:c16d9e9076a1 | 65 | writeData(_read_data, strlen(_read_data)); //required to call onReadable section |
wellmon7 | 3:c16d9e9076a1 | 66 | |
wellmon7 | 3:c16d9e9076a1 | 67 | LocalFileSystem local("local"); |
wellmon7 | 3:c16d9e9076a1 | 68 | |
wellmon7 | 3:c16d9e9076a1 | 69 | /*******************************INPUT VALIDATION*******************************/ |
wellmon7 | 3:c16d9e9076a1 | 70 | int u = 7; |
wellmon7 | 3:c16d9e9076a1 | 71 | //checks the submission for values that aren't accepted. Denies everything but letters. |
wellmon7 | 3:c16d9e9076a1 | 72 | while(u < strlen(myData)){ |
wellmon7 | 3:c16d9e9076a1 | 73 | if(iscntrl(myData[u]) !=0 || ispunct(myData[u])!=0 || isdigit(myData[u])!=0) { |
wellmon7 | 3:c16d9e9076a1 | 74 | pcx.printf("\nCONTROL VALUES FOUND\n"); |
wellmon7 | 3:c16d9e9076a1 | 75 | myData = "xxxxxxxsomeone entered bad code >:{\n"; |
wellmon7 | 3:c16d9e9076a1 | 76 | } |
wellmon7 | 3:c16d9e9076a1 | 77 | u++; |
wellmon7 | 3:c16d9e9076a1 | 78 | } |
wellmon7 | 3:c16d9e9076a1 | 79 | |
wellmon7 | 3:c16d9e9076a1 | 80 | /**************************END OF INPUT VALIDATION******************************/ |
wellmon7 | 3:c16d9e9076a1 | 81 | |
wellmon7 | 3:c16d9e9076a1 | 82 | //Copies submitted string into another buffer to get rid of 'variablename=' at the beginning |
wellmon7 | 3:c16d9e9076a1 | 83 | |
wellmon7 | 3:c16d9e9076a1 | 84 | char final[1024] = {}; |
wellmon7 | 3:c16d9e9076a1 | 85 | int i = 7; |
wellmon7 | 3:c16d9e9076a1 | 86 | int x = 0; |
wellmon7 | 3:c16d9e9076a1 | 87 | while(i < (strlen(myData))) { |
wellmon7 | 3:c16d9e9076a1 | 88 | final[x] = myData[i]; |
wellmon7 | 3:c16d9e9076a1 | 89 | i++; |
wellmon7 | 3:c16d9e9076a1 | 90 | x++; |
wellmon7 | 3:c16d9e9076a1 | 91 | } |
wellmon7 | 3:c16d9e9076a1 | 92 | |
wellmon7 | 3:c16d9e9076a1 | 93 | |
wellmon7 | 3:c16d9e9076a1 | 94 | //If submission =='new', copy data in the file that was appended to (myList.txt) and append it to a new file(log.txt). Delete myList.txt until new submission |
wellmon7 | 3:c16d9e9076a1 | 95 | |
wellmon7 | 3:c16d9e9076a1 | 96 | FILE * listFile; |
wellmon7 | 3:c16d9e9076a1 | 97 | char copyData[] = "new"; |
wellmon7 | 3:c16d9e9076a1 | 98 | if(strcmp(copyData, final)== 0){ |
wellmon7 | 3:c16d9e9076a1 | 99 | int size = 1; |
wellmon7 | 3:c16d9e9076a1 | 100 | int tsize = 1; |
wellmon7 | 3:c16d9e9076a1 | 101 | FILE * history; |
wellmon7 | 3:c16d9e9076a1 | 102 | listFile = fopen ("/local/myList.txt", "r"); |
wellmon7 | 3:c16d9e9076a1 | 103 | |
wellmon7 | 3:c16d9e9076a1 | 104 | char * bufRead[tsize]; |
wellmon7 | 3:c16d9e9076a1 | 105 | |
wellmon7 | 3:c16d9e9076a1 | 106 | history = fopen("/local/log.txt", "a"); |
wellmon7 | 3:c16d9e9076a1 | 107 | fprintf(history, "**********DAY MARKER**************\n\n"); |
wellmon7 | 3:c16d9e9076a1 | 108 | |
wellmon7 | 3:c16d9e9076a1 | 109 | while (size) { |
wellmon7 | 3:c16d9e9076a1 | 110 | size = fread(bufRead,1,1,listFile); |
wellmon7 | 3:c16d9e9076a1 | 111 | fwrite (bufRead , 1, 1, history); |
wellmon7 | 3:c16d9e9076a1 | 112 | } |
wellmon7 | 3:c16d9e9076a1 | 113 | fprintf(history, "\n\n"); |
wellmon7 | 3:c16d9e9076a1 | 114 | fclose(listFile); |
wellmon7 | 3:c16d9e9076a1 | 115 | fclose(history); |
wellmon7 | 3:c16d9e9076a1 | 116 | remove("/local/myList.txt"); |
wellmon7 | 3:c16d9e9076a1 | 117 | lcd.cls(); |
wellmon7 | 3:c16d9e9076a1 | 118 | lcd.printf(" A New Sheet\n Has Been Started\n WAITING..."); |
wellmon7 | 3:c16d9e9076a1 | 119 | } |
wellmon7 | 3:c16d9e9076a1 | 120 | else{ //Write submitted html data to file myList.txt |
wellmon7 | 3:c16d9e9076a1 | 121 | listFile = fopen("/local/myList.txt", "a"); |
wellmon7 | 3:c16d9e9076a1 | 122 | fprintf(listFile, "%s \n", final); |
wellmon7 | 3:c16d9e9076a1 | 123 | fclose(listFile); |
wellmon7 | 3:c16d9e9076a1 | 124 | char * show = strcat(final, " signed TAPs"); |
wellmon7 | 3:c16d9e9076a1 | 125 | lcd.cls(); |
wellmon7 | 3:c16d9e9076a1 | 126 | lcd.printf("%s\n WAITING... ",show); |
wellmon7 | 3:c16d9e9076a1 | 127 | } |
wellmon7 | 3:c16d9e9076a1 | 128 | } |
wellmon7 | 3:c16d9e9076a1 | 129 | } |
wellmon7 | 3:c16d9e9076a1 | 130 | |
wellmon7 | 3:c16d9e9076a1 | 131 | void PostHandler::onWriteable(){ |
wellmon7 | 3:c16d9e9076a1 | 132 | |
wellmon7 | 3:c16d9e9076a1 | 133 | close();} |
wellmon7 | 3:c16d9e9076a1 | 134 | |
wellmon7 | 3:c16d9e9076a1 | 135 | |
wellmon7 | 3:c16d9e9076a1 | 136 | void PostHandler::onClose(){ |
wellmon7 | 3:c16d9e9076a1 | 137 | return; |
wellmon7 | 3:c16d9e9076a1 | 138 | } |
wellmon7 | 3:c16d9e9076a1 | 139 |