Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <algorithm>
- #include <string.h>
- #include <sstream>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <netdb.h>
- #include <assert.h>
- using namespace std;
- const int BUFSIZE=100240;
- int MakeServerSocket(const char *port) {
- const int MAXNAMELEN = 255;
- const int BACKLOG = 3;
- char localhostname[MAXNAMELEN]; // local host name
- int s;
- int len;
- struct sockaddr_in sa;
- struct hostent *hp;
- struct servent *sp;
- int portnum;
- int ret;
- hp = gethostbyname("198.110.204.9");
- sscanf(port, "%d", &portnum);
- if (portnum == 0) {
- sp=getservbyname(port, "tcp");
- portnum = ntohs(sp->s_port);
- }
- sa.sin_port = htons(portnum);
- bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
- sa.sin_family = hp->h_addrtype;
- s = socket(hp->h_addrtype, SOCK_STREAM, 0);
- ret = bind(s, (struct sockaddr *)&sa, sizeof(sa));
- if (ret < 0)
- perror("Bad");
- listen(s, BACKLOG);
- cout << "Waiting for connection on port " << port << endl;
- return s;
- }
- string getContentType(string filename){
- string fileExt = "";
- size_t pos = filename.find(".");
- if(pos != string::npos) fileExt = filename.substr(pos+1);
- return fileExt;
- }
- string setContentType(string ct){
- string contentType = "";
- if(ct == "txt"){
- contentType = "text/plain";
- } else if(ct == "jpg"){
- contentType = "image/jpeg";
- } else if(ct == "html"){
- contentType = "text/html";
- }
- return contentType;
- }
- main(int argc, char *argv[]) {
- int s, len, ret;
- char buf[BUFSIZE];
- s = MakeServerSocket(argv[1]);
- while (1) {
- struct sockaddr_in sa;
- int sa_len = sizeof(sa);
- int fd = accept(s, (struct sockaddr *)&sa, (unsigned int *)&sa_len);
- assert(fd != -1);
- int len = read(fd, buf, BUFSIZE);
- cout << "------------Read says:" << len << "--------\n";
- buf[len] = 0;
- cout << buf << endl;
- string str = buf;
- string get, filename, version;
- string res = str + "\r\n";
- istringstream is (res);
- is >> get;
- is >> filename;
- is >> version;
- string vrsn = version.substr(5);
- string ct = getContentType(filename.substr(1));
- string fileExt = setContentType(ct);
- int in = open(filename.substr(1).c_str(), O_RDONLY);
- if (in == -1){
- string msg = "HTTP/1.0 404\r\nContent-Type: text/plain\r\n\r\n\r\nThe file was not found";
- write(fd, msg.c_str(), strlen(msg.c_str()));
- perror("Could not open input file");
- exit(1);
- }
- int fileLength = read(in, buf, BUFSIZE);
- buf[fileLength] = 0;
- char *fileContents = buf;
- while (fileLength > 0) {
- string msg = "HTTP/1.0 200 OK\r\nContent-Type:" + fileExt + "\r\n\r\n\r\nHere is response data";
- int bytes_written;
- if(vrsn == "1.1" || vrsn == "1.0"){
- write(fd, msg.c_str(), strlen(msg.c_str()));
- bytes_written = write(fd, fileContents, fileLength);
- } else {
- bytes_written = write(fd, fileContents, fileLength);
- }
- fileLength -= bytes_written;
- fileContents += bytes_written;
- }
- close(fd);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement