Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <stdarg.h>
- #include <sys/socket.h>
- #include <sys/param.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <errno.h>
- #include <vector>
- #include <strings.h>
- #include <sys/sem.h>
- #include <semaphore.h>
- #include <fcntl.h>
- #include <vector>
- struct file_lock
- {
- char filename[255];
- sem_t *sem;
- };
- std::vector<file_lock> lock;
- int read_line(int fd, void *buffer, size_t n)
- {
- int numRead; /* # of bytes fetched by last read() */
- int totRead; /* Total bytes read so far */
- char *buf;
- char ch;
- if (n <= 0 || buffer == NULL) {
- errno = EINVAL;
- return -1;
- }
- buf = (char*)buffer; /* No pointer arithmetic on "void *" */
- totRead = 0;
- for (;;) {
- numRead = read(fd, &ch, 1);
- if (numRead == -1) {
- if (errno == EINTR) /* Interrupted --> restart read() */
- continue;
- else
- return -1; /* Some other error */
- } else if (numRead == 0) { /* EOF */
- if (totRead == 0) /* No bytes read; return 0 */
- return 0;
- else /* Some bytes read; add '\0' */
- break;
- } else { /* 'numRead' must be 1 if we get here */
- if (totRead < n - 1) { /* Discard > (n - 1) bytes */
- totRead++;
- *buf++ = ch;
- }
- if (ch == '\n')
- break;
- }
- }
- *buf = '\0';
- return totRead;
- }
- struct name_samphore {
- char filename[256];
- bool semaphore;
- } ;
- int main(int argc, char *argv[])
- {
- char content_type[24] = "Content-Type: text/html";
- sockaddr_in inadr;
- inadr.sin_family = AF_INET;
- inadr.sin_port = htons( 12345 );
- inadr.sin_addr.s_addr = INADDR_ANY;
- int sd = socket( AF_INET, SOCK_STREAM, 0 );
- int err = bind( sd, ( sockaddr * ) &inadr, sizeof( inadr ) );
- if ( err < 0 ) printf( "bind selhal\n" );
- err = listen( sd, 1 );
- while(1)
- {
- sockaddr_in peeradr;
- socklen_t len_adr = sizeof( peeradr );
- int newsock = accept( sd, ( sockaddr * ) &peeradr, &len_adr );
- char data[ 256 ];
- char request_path[256];
- while((read_line( newsock, &data, sizeof( data )) > 0))
- {
- //printf("Reading the file\n");
- //printf(data);
- char *location = strstr(data,"GET");
- if (((int)location-(int)data+1) == 1)
- {
- // starts with GET
- //printf("Request starts with GET\n");
- char *location2 = strchr(data,'/');
- int index = (int)(location2 - data);
- int iter = 0;
- memset ( request_path, 0, 256 );
- do {
- request_path[iter] = data[index];
- iter++;
- index++;
- }
- while( data[index] != ' ' );
- printf("Requested path is %s\n", request_path);
- }
- if ( data[0] == '\n' || data[0] == '\r')
- {
- printf("New line delimiter\n");
- break;
- }
- bool isInList = false;
- // check if file exists in vector
- for (int i = 0; i < lock.size(); i++)
- {
- if(strcmp(lock[i].filename, request_path) == 0)
- {
- printf("file name of the sema match . . alread in vector \n");
- isInList = true;
- break;
- }
- }
- if(!isInList)
- {
- // if file does not exists add that semaphore into vector
- char sem_name[100];
- memset ( sem_name, 0, 100 );
- sprintf(sem_name,"%s", request_path);
- printf("Create semaphore %s \n", sem_name);
- sem_t *semik = sem_open(sem_name, O_CREAT | O_RDWR, 0660, 1);
- sem_init(semik, 1,1);
- file_lock new_lock;
- new_lock.sem = semik;
- sprintf(new_lock.filename, request_path);
- lock.push_back(new_lock);
- }
- }
- if (fork() == 0)
- {
- // creating response
- char response[255] = {0};
- printf("trzing to open %s", request_path+1);
- int fp = open(request_path+1, O_RDONLY);
- if (fp < 0)
- {
- printf("Cannot read file\n");
- sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n");
- write(newsock, response, strlen(response));
- close(newsock);
- exit(0);
- }
- // HTTP response
- sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Closed\r\n\r\n");
- write(newsock, response, strlen(response));
- char w_buff[64];
- int ret = 0;
- while((ret = read(fp, w_buff, 64)) > 0)
- {
- int res = write(newsock, w_buff, ret);
- if (res < 0)
- {
- printf("writing error\n");
- }
- }
- close(fp);
- printf("All data send. Closing socket.\n");
- close(newsock);
- exit(0);
- }
- else
- {
- printf("Closing parent socks\n");
- close( newsock );
- }
- usleep( 10 );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement