EString EHttpProxyRequest::toHttpString() { EString sb; sb.append(getHttpVerb()).append(' ').append(getHttpURI()).append(' ').append(getHttpVersion()) .append(EHttpProxyConstants_CRLF); boolean hostHeaderFound = false; if (getHeaders() != null) { sp<EIterator<EMapEntry<EString*, EList<EString*>*>*> > iter = getHeaders()->entrySet()->iterator(); while (iter->hasNext()) { EMapEntry<EString*, EList<EString*>*>* header = iter->next(); if (!hostHeaderFound) { hostHeaderFound = header->getKey()->equalsIgnoreCase("host"); } EList<EString*>* values = header->getValue(); if (values) { sp<EIterator<EString*> > iter2 = values->iterator(); while (iter2->hasNext()) { sb.append(header->getKey()->c_str()).append(": ").append(iter2->next()).append(EHttpProxyConstants_CRLF); } } } if (!hostHeaderFound && (eso_strcmp(getHttpVersion(), EHttpProxyConstants_HTTP_1_1) == 0)) { sb.append("Host: ").append(getHost()).append(EHttpProxyConstants_CRLF); } } sb.append(EHttpProxyConstants_CRLF); return sb; }
int handleHttpGET(char *input) { // IF NOT EXISTS // RETURN -1 // IF EXISTS // RETURN 1 char *filename = (char*)malloc(200 * sizeof(char)); char *path = (char*)malloc(1000 * sizeof(char)); char *extension = (char*)malloc(10 * sizeof(char)); char *mime = (char*)malloc(200 * sizeof(char)); char *httpVersion = (char*)malloc(20 * sizeof(char)); int contentLength = 0; int mimeSupported = 0; int fileNameLenght = 0; memset(path, '\0', 1000); memset(filename, '\0', 200); memset(extension, '\0', 10); memset(mime, '\0', 200); memset(httpVersion, '\0', 20); fileNameLenght = scan(input, filename, 5, 200); if ( fileNameLenght > 0 ) { if ( getHttpVersion(input, httpVersion) != -1 ) { FILE *fp; if ( GetExtension(filename, extension, 10) == -1 ) { printf("File extension not existing"); sendString("400 Bad Request\n", connecting_socket); free(filename); free(mime); free(path); free(extension); return -1; } mimeSupported = checkMime(extension, mime); if ( mimeSupported != 1) { printf("Mime not supported"); sendString("400 Bad Request\n", connecting_socket); free(filename); free(mime); free(path); free(extension); return -1; } // Open the requesting file as binary // strcpy(path, wwwroot); strcat(path, filename); fp = fopen(path, "rb"); if ( fp == NULL ) { printf("Unable to open file"); sendString("404 Not Found\n", connecting_socket); free(filename); free(mime); free(extension); free(path); return -1; } // Calculate Content Length // contentLength = Content_Lenght(fp); if (contentLength < 0 ) { printf("File size is zero"); free(filename); free(mime); free(extension); free(path); fclose(fp); return -1; } // Send File Content // sendHeader("200 OK", mime,contentLength, connecting_socket); sendFile(fp, contentLength); free(filename); free(mime); free(extension); free(path); fclose(fp); return 1; } else { sendString("501 Not Implemented\n", connecting_socket); } } return -1; }