Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <stdarg.h>
  7. #include <sys/socket.h>
  8. #include <sys/param.h>
  9. #include <sys/time.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <vector>
  16. #include <strings.h>
  17. #include <sys/sem.h>
  18. #include <semaphore.h>
  19. #include <fcntl.h>
  20. #include <vector>
  21.  
  22.  
  23.  
  24. struct file_lock
  25. {
  26. char filename[255];
  27. sem_t *sem;
  28. };
  29. std::vector<file_lock> lock;
  30.  
  31.  
  32.  
  33.  
  34. int read_line(int fd, void *buffer, size_t n)
  35. {
  36. int numRead; /* # of bytes fetched by last read() */
  37. int totRead; /* Total bytes read so far */
  38. char *buf;
  39. char ch;
  40.  
  41. if (n <= 0 || buffer == NULL) {
  42. errno = EINVAL;
  43. return -1;
  44. }
  45.  
  46. buf = (char*)buffer; /* No pointer arithmetic on "void *" */
  47.  
  48. totRead = 0;
  49. for (;;) {
  50. numRead = read(fd, &ch, 1);
  51.  
  52. if (numRead == -1) {
  53. if (errno == EINTR) /* Interrupted --> restart read() */
  54. continue;
  55. else
  56. return -1; /* Some other error */
  57.  
  58. } else if (numRead == 0) { /* EOF */
  59. if (totRead == 0) /* No bytes read; return 0 */
  60. return 0;
  61. else /* Some bytes read; add '\0' */
  62. break;
  63.  
  64. } else { /* 'numRead' must be 1 if we get here */
  65. if (totRead < n - 1) { /* Discard > (n - 1) bytes */
  66. totRead++;
  67. *buf++ = ch;
  68. }
  69.  
  70. if (ch == '\n')
  71. break;
  72. }
  73. }
  74.  
  75. *buf = '\0';
  76. return totRead;
  77. }
  78.  
  79. struct name_samphore {
  80. char filename[256];
  81. bool semaphore;
  82. } ;
  83.  
  84.  
  85. int main(int argc, char *argv[])
  86. {
  87.  
  88. char content_type[24] = "Content-Type: text/html";
  89.  
  90. sockaddr_in inadr;
  91. inadr.sin_family = AF_INET;
  92. inadr.sin_port = htons( 12345 );
  93. inadr.sin_addr.s_addr = INADDR_ANY;
  94. int sd = socket( AF_INET, SOCK_STREAM, 0 );
  95. int err = bind( sd, ( sockaddr * ) &inadr, sizeof( inadr ) );
  96. if ( err < 0 ) printf( "bind selhal\n" );
  97. err = listen( sd, 1 );
  98.  
  99.  
  100. while(1)
  101. {
  102. sockaddr_in peeradr;
  103. socklen_t len_adr = sizeof( peeradr );
  104. int newsock = accept( sd, ( sockaddr * ) &peeradr, &len_adr );
  105.  
  106. char data[ 256 ];
  107. char request_path[256];
  108. while((read_line( newsock, &data, sizeof( data )) > 0))
  109. {
  110. //printf("Reading the file\n");
  111. //printf(data);
  112. char *location = strstr(data,"GET");
  113.  
  114. if (((int)location-(int)data+1) == 1)
  115. {
  116.  
  117. // starts with GET
  118. //printf("Request starts with GET\n");
  119. char *location2 = strchr(data,'/');
  120. int index = (int)(location2 - data);
  121. int iter = 0;
  122.  
  123. memset ( request_path, 0, 256 );
  124.  
  125. do {
  126.  
  127. request_path[iter] = data[index];
  128. iter++;
  129. index++;
  130. }
  131. while( data[index] != ' ' );
  132.  
  133. printf("Requested path is %s\n", request_path);
  134.  
  135. }
  136. if ( data[0] == '\n' || data[0] == '\r')
  137. {
  138. printf("New line delimiter\n");
  139. break;
  140.  
  141. }
  142.  
  143. bool isInList = false;
  144. // check if file exists in vector
  145. for (int i = 0; i < lock.size(); i++)
  146. {
  147. if(strcmp(lock[i].filename, request_path) == 0)
  148. {
  149. printf("file name of the sema match . . alread in vector \n");
  150. isInList = true;
  151. break;
  152. }
  153. }
  154.  
  155. if(!isInList)
  156. {
  157. // if file does not exists add that semaphore into vector
  158. char sem_name[100];
  159. memset ( sem_name, 0, 100 );
  160. sprintf(sem_name,"%s", request_path);
  161.  
  162. printf("Create semaphore %s \n", sem_name);
  163.  
  164. sem_t *semik = sem_open(sem_name, O_CREAT | O_RDWR, 0660, 1);
  165. sem_init(semik, 1,1);
  166. file_lock new_lock;
  167. new_lock.sem = semik;
  168. sprintf(new_lock.filename, request_path);
  169. lock.push_back(new_lock);
  170. }
  171.  
  172. }
  173.  
  174. if (fork() == 0)
  175. {
  176.  
  177.  
  178. // creating response
  179.  
  180.  
  181.  
  182. char response[255] = {0};
  183. printf("trzing to open %s", request_path+1);
  184. int fp = open(request_path+1, O_RDONLY);
  185.  
  186. if (fp < 0)
  187. {
  188. printf("Cannot read file\n");
  189. sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n");
  190. write(newsock, response, strlen(response));
  191.  
  192. close(newsock);
  193. exit(0);
  194. }
  195.  
  196. // HTTP response
  197. sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n");
  198. write(newsock, response, strlen(response));
  199.  
  200. char w_buff[64];
  201. int ret = 0;
  202. while((ret = read(fp, w_buff, 64)) > 0)
  203. {
  204. int res = write(newsock, w_buff, ret);
  205. if (res < 0)
  206. {
  207. printf("writing error\n");
  208. }
  209. }
  210. close(fp);
  211.  
  212. printf("All data send. Closing socket.\n");
  213. close(newsock);
  214. exit(0);
  215.  
  216.  
  217. }
  218. else
  219. {
  220. printf("Closing parent socks\n");
  221. close( newsock );
  222. }
  223.  
  224.  
  225. usleep( 10 );
  226. }
  227.  
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement