Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include <string.h>
  5. #include <sstream>
  6. #include <netinet/in.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <netdb.h>
  13. #include <assert.h>
  14.  
  15. using namespace std;
  16.  
  17. const int BUFSIZE=100240;
  18.  
  19. int MakeServerSocket(const char *port) {
  20. const int MAXNAMELEN = 255;
  21. const int BACKLOG = 3;
  22. char localhostname[MAXNAMELEN]; // local host name
  23. int s;
  24. int len;
  25. struct sockaddr_in sa;
  26. struct hostent *hp;
  27. struct servent *sp;
  28. int portnum;
  29. int ret;
  30. hp = gethostbyname("198.110.204.9");
  31. sscanf(port, "%d", &portnum);
  32. if (portnum == 0) {
  33. sp=getservbyname(port, "tcp");
  34. portnum = ntohs(sp->s_port);
  35. }
  36. sa.sin_port = htons(portnum);
  37.  
  38. bcopy((char *)hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  39. sa.sin_family = hp->h_addrtype;
  40.  
  41. s = socket(hp->h_addrtype, SOCK_STREAM, 0);
  42.  
  43. ret = bind(s, (struct sockaddr *)&sa, sizeof(sa));
  44. if (ret < 0)
  45. perror("Bad");
  46. listen(s, BACKLOG);
  47. cout << "Waiting for connection on port " << port << endl;
  48. return s;
  49. }
  50.  
  51. string getContentType(string filename){
  52. string fileExt = "";
  53. size_t pos = filename.find(".");
  54. if(pos != string::npos) fileExt = filename.substr(pos+1);
  55. return fileExt;
  56. }
  57.  
  58. string setContentType(string ct){
  59. string contentType = "";
  60. if(ct == "txt"){
  61. contentType = "text/plain";
  62. } else if(ct == "jpg"){
  63. contentType = "image/jpeg";
  64. } else if(ct == "html"){
  65. contentType = "text/html";
  66. }
  67.  
  68. return contentType;
  69. }
  70.  
  71. main(int argc, char *argv[]) {
  72.  
  73. int s, len, ret;
  74. char buf[BUFSIZE];
  75. s = MakeServerSocket(argv[1]);
  76.  
  77. while (1) {
  78. struct sockaddr_in sa;
  79. int sa_len = sizeof(sa);
  80. int fd = accept(s, (struct sockaddr *)&sa, (unsigned int *)&sa_len);
  81. assert(fd != -1);
  82.  
  83. int len = read(fd, buf, BUFSIZE);
  84. cout << "------------Read says:" << len << "--------\n";
  85. buf[len] = 0;
  86. cout << buf << endl;
  87.  
  88. string str = buf;
  89. string get, filename, version;
  90. string res = str + "\r\n";
  91. istringstream is (res);
  92. is >> get;
  93. is >> filename;
  94. is >> version;
  95.  
  96. string vrsn = version.substr(5);
  97. string ct = getContentType(filename.substr(1));
  98. string fileExt = setContentType(ct);
  99. int in = open(filename.substr(1).c_str(), O_RDONLY);
  100.  
  101. if (in == -1){
  102. string msg = "HTTP/1.0 404\r\nContent-Type: text/plain\r\n\r\n\r\nThe file was not found";
  103. write(fd, msg.c_str(), strlen(msg.c_str()));
  104. perror("Could not open input file");
  105. exit(1);
  106. }
  107.  
  108. int fileLength = read(in, buf, BUFSIZE);
  109. buf[fileLength] = 0;
  110. char *fileContents = buf;
  111.  
  112. while (fileLength > 0) {
  113.  
  114. string msg = "HTTP/1.0 200 OK\r\nContent-Type:" + fileExt + "\r\n\r\n\r\nHere is response data";
  115. int bytes_written;
  116.  
  117. if(vrsn == "1.1" || vrsn == "1.0"){
  118. write(fd, msg.c_str(), strlen(msg.c_str()));
  119. bytes_written = write(fd, fileContents, fileLength);
  120. } else {
  121. bytes_written = write(fd, fileContents, fileLength);
  122. }
  123.  
  124. fileLength -= bytes_written;
  125. fileContents += bytes_written;
  126.  
  127. }
  128.  
  129. close(fd);
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement