src/server/safs/service.c

changeset 77
f1cff81e425a
parent 70
4e6e812c1d97
child 87
bdec069d2239
equal deleted inserted replaced
76:5f7660fe1562 77:f1cff81e425a
50 * return the opened file 50 * return the opened file
51 */ 51 */
52 SYS_FILE prepare_service_file(Session *sn, Request *rq, struct stat *s) { 52 SYS_FILE prepare_service_file(Session *sn, Request *rq, struct stat *s) {
53 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars); 53 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
54 54
55 /* open the file */ 55 // open the file
56 VFSContext *vfs = vfs_request_context(sn, rq); 56 VFSContext *vfs = vfs_request_context(sn, rq);
57 SYS_FILE fd = vfs_open(vfs, ppath, O_RDONLY); 57 SYS_FILE fd = vfs_open(vfs, ppath, O_RDONLY);
58 if(!fd) { 58 if(!fd) {
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 } 68 }
82 http_start_response(sn, rq); 82 http_start_response(sn, rq);
83 vfs_close(fd); 83 vfs_close(fd);
84 return fd; 84 return fd;
85 } 85 }
86 86
87 /* add content-length header*/ 87 // add content-length header
88 char contentLength[32]; 88 char contentLength[32];
89 int len = snprintf(contentLength, 32, "%lld", s->st_size); 89 int len = snprintf(contentLength, 32, "%lld", s->st_size);
90 90
91 pblock_kvinsert(pb_key_content_length, contentLength, len, rq->srvhdrs); 91 pblock_kvinsert(pb_key_content_length, contentLength, len, rq->srvhdrs);
92 92
93 /* start response */ 93 // start response
94 protocol_status(sn, rq, 200, NULL); 94 protocol_status(sn, rq, 200, NULL);
95 http_start_response(sn, rq); 95 http_start_response(sn, rq);
96 96
97 return fd; 97 return fd;
98 } 98 }
138 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars); 138 char *ppath = pblock_findkeyval(pb_key_ppath, rq->vars);
139 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); 139 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
140 140
141 sstr_t r_uri = sstr(uri); 141 sstr_t r_uri = sstr(uri);
142 142
143 /* open the file */ 143 // open the file
144 VFSContext *vfs = vfs_request_context(sn, rq); 144 VFSContext *vfs = vfs_request_context(sn, rq);
145 VFS_DIR dir = vfs_opendir(vfs, ppath); 145 VFS_DIR dir = vfs_opendir(vfs, ppath);
146 if(!dir) { 146 if(!dir) {
147 return REQ_ABORTED; 147 return REQ_ABORTED;
148 } 148 }
149 149
150 sbuf_t *out = sbuf_new(1024); /* output buffer */ 150 sbuf_t *out = sbuf_new(1024); // output buffer
151 151
152 /* write html header */ 152 // write html header
153 sbuf_puts(out, "<html>\n<head>\n<title>Index of "); 153 sbuf_puts(out, "<html>\n<head>\n<title>Index of ");
154 sbuf_puts(out, uri); 154 sbuf_puts(out, uri);
155 sbuf_puts(out, "</title>\n</head><body>\n<h1>Index of "); 155 sbuf_puts(out, "</title>\n</head><body>\n<h1>Index of ");
156 sbuf_puts(out, uri); 156 sbuf_puts(out, uri);
157 sbuf_puts(out, "</h1><hr>\n\n"); 157 sbuf_puts(out, "</h1><hr>\n\n");
158 158
159 //struct dirent *f; 159 // list directory
160 VFS_ENTRY f; 160 VFS_ENTRY f;
161 while(vfs_readdir(dir, &f)) { 161 while(vfs_readdir(dir, &f)) {
162 sstr_t filename = sstr(f.name); 162 sstr_t filename = sstr(f.name);
163 163
164 sbuf_puts(out, "<a href=\""); 164 sbuf_puts(out, "<a href=\"");
169 sbuf_puts(out, "</a><br>\n"); 169 sbuf_puts(out, "</a><br>\n");
170 } 170 }
171 171
172 sbuf_puts(out, "\n</body>\n</html>\n"); 172 sbuf_puts(out, "\n</body>\n</html>\n");
173 173
174 /* send stuff to client */ 174 // send stuff to client
175 pblock_removekey(pb_key_content_type, rq->srvhdrs); 175 pblock_removekey(pb_key_content_type, rq->srvhdrs);
176 pblock_kvinsert(pb_key_content_type, "text/html", 9, rq->srvhdrs); 176 pblock_kvinsert(pb_key_content_type, "text/html", 9, rq->srvhdrs);
177 pblock_nninsert("content-length", out->length, rq->srvhdrs); 177 pblock_nninsert("content-length", out->length, rq->srvhdrs);
178 protocol_status(sn, rq, 200, NULL); 178 protocol_status(sn, rq, 200, NULL);
179 http_start_response(sn, rq); 179 http_start_response(sn, rq);
180 180
181 net_write(sn->csd, out->ptr, out->length); 181 net_write(sn->csd, out->ptr, out->length);
182 182
183 /* close */ 183 // close
184 vfs_closedir(dir); 184 vfs_closedir(dir);
185 sbuf_free(out); 185 sbuf_free(out);
186 186
187 return REQ_PROCEED; 187 return REQ_PROCEED;
188 } 188 }

mercurial