Skip to content

Commit

Permalink
Added basic directory listing and 301 responses fixing #32.
Browse files Browse the repository at this point in the history
  • Loading branch information
triforce committed Nov 3, 2017
1 parent 30b1ad8 commit 4e61959
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

all: main

release: http.asm constants.asm bss.asm data.asm macros.asm main.asm mutex.asm string.asm syscall.asm
release: http.asm constants.asm bss.asm data.asm macros.asm main.asm mutex.asm string.asm syscall.asm dirent.asm
yasm -f elf64 -a x86 main.asm -o main.o
ld main.o -o asmttpd
strip -s asmttpd

main.o: http.asm constants.asm bss.asm data.asm macros.asm main.asm mutex.asm string.asm syscall.asm
main.o: http.asm constants.asm bss.asm data.asm macros.asm main.asm mutex.asm string.asm syscall.asm dirent.asm
yasm -g dwarf2 -f elf64 -a x86 main.asm -o main.o
main: main.o
ld main.o -o asmttpd
Expand Down
13 changes: 13 additions & 0 deletions constants.asm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
%define THREAD_BUFFER_SIZE 8192 ; 8KB recv buffer
%define URL_LENGTH_LIMIT 2000
%define DIRECTORY_LENGTH_LIMIT 100
%define DIRECTORY_LIST_BUFFER 1024 ; holds linux_dirents
%define CUSTOM_CONTENT_BUFFER 1024 ; holds directory listing HTML formatted reply

;Request Types
%define REQ_UNK 0
Expand Down Expand Up @@ -76,6 +78,17 @@
%define CONTENT_TYPE_PNG 7
%define CONTENT_TYPE_JPEG 8

;Dirent types
%define DT_UNKNOWN 0
%define DT_FIFO 1
%define DT_CHR 2
%define DT_DIR 4
%define DT_BLK 6
%define DT_REG 8
%define DT_LNK 10
%define DT_SOCK 12
%define DT_WHT 14

;System Call Values
%define SYS_WRITE 1 ;int fd, const void *buf, size_t count
%define SYS_LSEEK 8 ;int fd, off_t offset, int whence
Expand Down
14 changes: 14 additions & 0 deletions data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
sa_flags dq SA_RESTORER ; also dq, because padding
sa_restorer dq 0
sa_mask dq 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

dir_ent_pointer dq 0
dir_ent_size dq 0
;Strings
in_enter db "In Enter:",0x00
Expand All @@ -56,6 +59,8 @@
header_range_search db "Range: ",0x00
header_range_search_len equ $ - header_range_search

location db "Location: ",0x00

find_bytes_range db "bytes=",0x00
find_bytes_range_len equ $ - find_bytes_range
Expand All @@ -76,6 +81,8 @@
http_200_len equ $ - http_200
http_206 db "HTTP/1.1 206 Partial Content",0x0d,0x0a,0x00
http_206_len equ $ - http_206
http_301 db "HTTP/1.1 301 Moved Permanently",0x0d,0x0a,0x00
http_301_len equ $ - http_301
http_404 db "HTTP/1.1 404 Not Found",0x0d,0x0a,0x00
http_404_len equ $ - http_404
http_404_text db "I'm sorry, Dave. I'm afraid I can't do that. 404 Not Found",0x00
Expand Down Expand Up @@ -147,3 +154,10 @@
extension_jpg db ".jpg",0x00
extension_jpeg db ".jpeg",0x00
extension_png db ".png",0x00

; dir listing
http_200_dir_list_open_h1_tag db "<h1>Index of ",0x00
http_200_dir_list_close_h1_tag db "</h1>",0x00
http_dir_entry_open_a_tag_pre db '<p><a href="',0x00
http_dir_entry_open_a_tag_post db '">',0x00
http_dir_entry_close_a_tag db '</a></p>',0x00
124 changes: 124 additions & 0 deletions dirent.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
;asmttpd - Web server for Linux written in amd64 assembly.
;Copyright (C) 2014 nemasu <[email protected]>
;
;This file is part of asmttpd.
;
;asmttpd is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 2 of the License, or
;(at your option) any later version.
;
;asmttpd is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

; d_inode = off + 0
; d_offset = off + 8
; d_reclen = off + 16
; d_name = off + 18
; null byte
; d_type = reclen - 1

; Get total number of getdents entries
get_dir_ents_number: ;ret rax entries
stackpush

xor rax, rax
xor rcx, rcx ; hold offset to current reclen entry
xor rbx, rbx ; hold total number of entries read
xor rdx, rdx ; hold total reclen bytes read
mov rcx, 16 ; point to first reclen offset

read_dir_ents_reclen:
mov rsi, [dir_ent_pointer] ; points to linux_dirents buffer
add rsi, rcx
lodsw ; ax now contains length of current entry
inc rbx
add rcx, rax
add rdx, rax
cmp rdx, [dir_ent_size]
jne read_dir_ents_reclen

mov rax, rbx
stackpop
ret

; Add a list of HTML formatted entries into a buffer
generate_dir_entries: ;rdi - buffer, ret - rax length of string in buffer
stackpush
; Add heading
mov rsi, http_200_dir_list_open_h1_tag
call string_concat
; Add /<dir_path>/ into heading contents
mov rsi, [rbp-16]
add rsi, r9
call string_concat
mov rsi, [directory_path]
call string_remove
; Close heading
mov rsi, http_200_dir_list_close_h1_tag
call string_concat

mov rsi, crlf
call string_concat

xor rcx, rcx ; hold offset to current reclen entry
xor rbx, rbx ; hold total number of entries read
xor rdx, rdx ; hold total reclen bytes read
mov rcx, 16 ; point to first reclen offset

read_dir_ents:
mov rsi, [dir_ent_pointer] ; points to linux_dirents buffer
add rsi, rcx
lodsw ; ax now contains length of current entry
inc rbx
add rcx, rax
add rdx, rax

; ignore the '.' entry
cmp word[rsi], 0x002e
je read_dir_ents_end

mov rbx, rsi
push rbx ; save for second time

push rsi
mov rsi, http_dir_entry_open_a_tag_pre
call string_concat
pop rsi

call string_concat ; add name, prepend with request dir

cmp word[rbx], 0x2e2e ; check for '..'
jne finish_link_tag
push rsi
mov rsi, char_slash
call string_concat
pop rsi

finish_link_tag:
push rsi
mov rsi, http_dir_entry_open_a_tag_post
call string_concat
pop rsi

pop rbx
mov rsi, rbx
call string_concat ; add name again

push rsi
mov rsi, http_dir_entry_close_a_tag
call string_concat
pop rsi

read_dir_ents_end:
cmp rdx, [dir_ent_size]
jne read_dir_ents

call get_string_length
stackpop
ret
37 changes: 37 additions & 0 deletions http.asm
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,43 @@ create_http404_response: ;rdi - pointer to buffer
stackpop
ret

create_http301_response: ;rdi - pointer to buffer
stackpush

mov rsi, http_301 ;First one we copy
mov rdx, http_301_len
call string_copy

mov rsi, server_header
call string_concat

mov rsi, r10
call add_content_type_header

mov rsi, connection_header
call string_concat

mov rsi, location
call string_concat

; Remove directory path (web_root) from buffer
mov rsi, [rbp-16]
add rsi, r9
call string_concat
mov rsi, [directory_path]
call string_remove

mov rsi, char_slash
call string_concat

mov rsi, crlf
call string_concat

call get_string_length

stackpop
ret

get_request_type: ;rdi - pointer to buffer, ret = rax: request type
stackpush

Expand Down
Loading

0 comments on commit 4e61959

Please sign in to comment.