src/server/safs/service.c

changeset 59
ab25c0a231d0
parent 58
66c22e54aa90
child 69
4a10bc0ee80d
equal deleted inserted replaced
58:66c22e54aa90 59:ab25c0a231d0
59 // vfs_open sets http status code 59 // vfs_open sets http status code
60 return NULL; 60 return NULL;
61 } 61 }
62 62
63 /* get stat */ 63 /* get stat */
64 if (vfs_fstat(vfs, fd, s) != 0) { 64 if(vfs_fstat(vfs, fd, s) != 0) {
65 //perror("prepare_service_file: stat"); 65 //perror("prepare_service_file: stat");
66 protocol_status(sn, rq, 500, NULL); 66 protocol_status(sn, rq, 500, NULL);
67 return NULL; 67 return NULL;
68 }
69
70 // check if the file is a directory
71 if(S_ISDIR(s->st_mode)) {
72 pblock_nvinsert("content-length", "0", rq->srvhdrs);
73 pblock_removekey(pb_key_content_type, rq->srvhdrs);
74 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
75 size_t urilen = strlen(uri);
76 char *location = pool_malloc(sn->pool, urilen + 2);
77 memcpy(location, uri, urilen);
78 location[urilen] = '/';
79 location[urilen+1] = '\0';
80 pblock_kvinsert(pb_key_location, location, urilen + 1, rq->srvhdrs);
81 protocol_status(sn, rq, 302, NULL);
82 http_start_response(sn, rq);
83 vfs_close(fd);
84 return fd;
68 } 85 }
69 86
70 /* add content-length header*/ 87 /* add content-length header*/
71 char contentLength[32]; 88 char contentLength[32];
72 int len = snprintf(contentLength, 32, "%d", s->st_size); 89 int len = snprintf(contentLength, 32, "%d", s->st_size);
87 // if an error occurs, prepare_service_file sets the http status code 104 // if an error occurs, prepare_service_file sets the http status code
88 // we can just return REQ_ABORTED 105 // we can just return REQ_ABORTED
89 return REQ_ABORTED; 106 return REQ_ABORTED;
90 } 107 }
91 108
92 // send file 109 if(!S_ISDIR(s.st_mode)) {
93 sendfiledata sfd; 110 // send file
94 sfd.fd = fd; 111 sendfiledata sfd;
95 sfd.len = s.st_size; 112 sfd.fd = fd;
96 sfd.offset = 0; 113 sfd.len = s.st_size;
97 sfd.header = NULL; 114 sfd.offset = 0;
98 sfd.trailer = NULL; 115 sfd.header = NULL;
99 net_sendfile(sn->csd, &sfd); 116 sfd.trailer = NULL;
117 net_sendfile(sn->csd, &sfd);
118 }
100 119
101 vfs_close(fd); 120 vfs_close(fd);
102 121
103 return REQ_PROCEED; 122 return REQ_PROCEED;
104 } 123 }

mercurial