Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Application: basic-webserver, Web.cpp
- * Author: Project, UNX511, Team A
- * Description: Web class - responsible for creating sockets and listening for connections.
- * A webserver that has been coded from scratch.
- * It uses Linux's socket library to deal with connections.
- *
- * This application is licensed under Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
- * See this for more information: http://creativecommons.org/licenses/by-nc-sa/3.0/
- */
- #include "Web.h"
- Web::Web(int port) {
- _port = port;
- _socket = socket(AF_INET, SOCK_STREAM, 0);
- if (_socket < 0) {
- log(ERROR, "System Call", "Socket", 0);
- }
- bzero((char *) &_serv_addr, sizeof(_serv_addr));
- _serv_addr.sin_family = AF_INET;
- _serv_addr.sin_addr.s_addr = INADDR_ANY;
- _serv_addr.sin_port = htons(_port);
- if (bind(_socket, (struct sockaddr*) &_serv_addr, sizeof(_serv_addr)) < 0) {
- log(ERROR, "System Call", "Bind", 0);
- }
- if (listen(_socket, 5) == -1){
- log(ERROR, "System Call", "Listen", 0);
- }
- _clilen = sizeof(_cli_addr);
- }
- int Web::run() {
- pid_t pid;
- while (1) {
- _nSocket = accept(_socket, (struct sockaddr *) &_cli_addr, &_clilen);
- if (_nSocket < 0) {
- log(ERROR, "System Call", "Accept", 0);
- }
- pid = fork();
- if (pid < 0) {
- log(ERROR, "System Call", "Fork", 0);
- }
- if (pid == 0) {
- if (close(_socket) == -1) {
- log(ERROR, "System Call", "Close", 0);
- }
- process(_nSocket);
- return EXIT_SUCCESS;
- } else {
- if (close(_nSocket) == -1) {
- log(ERROR, "System Call", "Close", 0);
- }
- }
- }
- if (close(_socket) == -1) {
- log(ERROR, "System Call", "Close", 0);
- }
- return EXIT_SUCCESS;
- }
- void Web::process(int socket) {
- int n;
- int size = 0;
- char buf[BUFSIZE+1];
- bzero(buf, BUFSIZE+1);
- n = read(socket, buf, BUFSIZE);
- if (n < 0) {
- log(ERROR, "System Call", "Socket Read", 0);
- }
- string response = buf;
- string get = "GET";
- string get_value;
- string cookie = "Cookie";
- string cookie_value;
- string tmp1;
- char* response_c = (char*)response.c_str();
- char* pch;
- char* pch2;
- pch = strtok(response_c, "\n");
- while(pch){
- tmp1 = pch;
- if(isHttpResponseOf(tmp1, get)){
- pch2 = strtok(pch, " ");
- pch2 = strtok(NULL, " "); // move once - the get_value is in the second token
- get_value = pch2;
- }
- else if(isHttpResponseOf(tmp1, cookie)){
- pch2 = strtok(pch, ":");
- cookie_value = pch2;
- cout << cookie_value << endl;
- }
- pch = strtok(NULL, "\n");
- }
- string
- html = HTTP_GENERIC_HEADER; // defined in Web.h
- html += "<html><head><title>Basic Web Server</title><script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></script><script type=\"text/javascript\">$(document).ready(function(){$('#msgInput').focus();});</script></head>";
- html += "<html><head><title>Basic Web Server</title></head><body><h1>Basic Web Server</h1>";
- html += "<textarea rows=\"50px\" cols=\"100px\" name=\"msgBox\" id=\"msgBox\" disabled=\"disabled\"></textarea><br /><form><input type=\"text\" size=\"131\" name=\"msgInput\" id=\"msgInput\" />";
- html += "<p>Hi World. Here's the response I got from you: </p><pre>" + response + "</pre>";
- html += "</body></html>\n";
- n = write(socket, html.c_str(), html.size());
- if (n < 0) {
- log(ERROR, "System Call", "Socket Write", 0);
- }
- if (close(socket) == -1) {
- log(ERROR, "System Call", "Close", 0);
- }
- }
- bool Web::isHttpResponseOf(string haystack, string needle){
- size_t found = haystack.find(needle);
- return found != string::npos;
- }
- void Web::log(int type, char* s1, char* s2, int num) {
- int fd;
- char logbuffer[BUFSIZE*2];
- switch (type) {
- case ERROR: sprintf(logbuffer,"ERROR: %s: %s Errno=%d exiting pid=%d", s1, s2, errno, getpid()); break;
- case LOG: sprintf(logbuffer," INFO: %s: %s: %d", s1, s2, num); break;
- }
- /* no checks here, nothing can be done a failure anyway */
- if((fd = open("server.log", O_CREAT | O_WRONLY | O_APPEND,0644)) >= 0) {
- write(fd, logbuffer, strlen(logbuffer));
- write(fd,"\n",1);
- close(fd);
- }
- if(type == ERROR) {
- exit(EXIT_FAILURE);
- }
- }
- Web::Web() { }
- Web::~Web() {
- close(_socket);
- close(_nSocket);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement