Skip to content

Commit

Permalink
Merge branch 'master' into add_dir_listing
Browse files Browse the repository at this point in the history
  • Loading branch information
triforce authored Jun 25, 2019
2 parents 00f5bf4 + 240ff27 commit 3bfa0dd
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 48 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ What works:
* Serving files from specified document root.
* HEAD requests.
* 200, 206, 404, 400, 413, 416
* Content-types: xml, html, xhtml, gif, png, jpeg, css, js, and octet-stream.
* Content-types: xml, html, xhtml, gif, png, jpeg, css, js, svg, and octet-stream.

Planned Features:
* Directory listing.
Expand All @@ -32,10 +32,19 @@ You will need `yasm` installed.
Usage
=======

`sudo ./asmttpd /path/to/web_root`
`./asmttpd /path/to/web_root port_number`

Example: `./asmttpd ./web_root 8080`

Changes
=======
2019-04-22 : asmttpd - 0.4.4

* Added SVG support.

2019-01-24 : asmttpd - 0.4.3

* Added port number as parameter.

2017-10-18 : asmttpd - 0.4.2

Expand Down
2 changes: 2 additions & 0 deletions bss.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.
listen_socket: resq 1
listen_port: resw 1
one_constant: resq 1
directory_path: resq 1
1 change: 1 addition & 0 deletions constants.asm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
%define CONTENT_TYPE_GIF 6
%define CONTENT_TYPE_PNG 7
%define CONTENT_TYPE_JPEG 8
%define CONTENT_TYPE_SVG 9

;Dirent types
%define DT_UNKNOWN 0
Expand Down
30 changes: 20 additions & 10 deletions data.asm
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

sockaddr_in: ;struct
sin_family dw AF_INET
sin_port dw LISTEN_PORT
sin_addr dd 0 ;INADDR_ANY
directory_path dq 0
request_type dq 0
request_offset dq 0
struc sockaddr_in
sin_family: resw 1
sin_port: resw 1
sin_addr: resd 1
endstruc
sa: istruc sockaddr_in
at sin_family, dw AF_INET
at sin_port, dw 0
at sin_addr, dd 0 ;INADDR_ANY
iend

request_type dq 0
request_offset dq 0
timeval: ;struct
tv_sec dq 0
tv_usec dq 0
Expand All @@ -47,10 +55,8 @@
msg_bind_error_len equ $ - msg_bind_error
msg_error db "An error has occured, exiting",0x00
msg_error_len equ $ - msg_error
msg_help db "Usage: ./asmttpd /path/to/directory",0x00
msg_help db "Usage: ./asmttpd /path/to/directory port",0x00
msg_help_len equ $ - msg_help
msg_using_directory dd "Using Document Root: ",0x00
msg_using_directory_len equ $ - msg_using_directory
msg_not_a_directory dd "Error: Specified document root is not a directory",0x00
msg_not_a_directory_len equ $ - msg_not_a_directory
msg_request_log db 0x0a,"Request: ",0x00
Expand Down Expand Up @@ -140,6 +146,9 @@
content_type_gif db "image/gif",0x0d,0x0a,0x00
content_type_gif_len equ $ - content_type_gif

content_type_svg db "image/svg+xml",0x0d,0x0a,0x00
content_type_svg_len equ $ - content_type_svg

default_document db "/index.html",0x00
default_document_len equ $ - default_document
Expand All @@ -154,6 +163,7 @@
extension_jpg db ".jpg",0x00
extension_jpeg db ".jpeg",0x00
extension_png db ".png",0x00
extension_svg db ".svg",0x00

; dir listing
http_200_dir_list_open_h1_tag db "<h1>Index of ",0x00
Expand Down
4 changes: 4 additions & 0 deletions debug.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
;
;You should have received a copy of the GNU General Public License
;along with asmttpd. If not, see <http://www.gnu.org/licenses/>.

;hint, use these with GDB
;set follow-fork-mode child

section .bss
printbuffer: resb 1024;debug

Expand Down
35 changes: 24 additions & 11 deletions http.asm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ detect_content_type: ;rdi - pointer to buffer that contains request, ret - rax:
cmp rax, 1
je detect_content_type_ret

mov rsi, extension_svg
call string_ends_with
mov r10, CONTENT_TYPE_SVG
cmp rax, 1
je detect_content_type_ret

mov r10, CONTENT_TYPE_OCTET_STREAM ; default to octet-stream
detect_content_type_ret:
mov rax, r10
Expand Down Expand Up @@ -112,6 +118,8 @@ add_content_type_header: ;rdi - pointer to buffer, rsi - type
je add_response_png
cmp r10, CONTENT_TYPE_JPEG
je add_response_jpeg
cmp r10, CONTENT_TYPE_SVG
je add_response_svg
jmp add_response_octet_stream

Expand Down Expand Up @@ -159,6 +167,11 @@ add_content_type_header: ;rdi - pointer to buffer, rsi - type
mov rsi, content_type_jpeg
call string_concat

add_response_svg:
mov rsi, content_type_svg
call string_concat


add_response_cont:
stackpop
ret
Expand Down Expand Up @@ -386,17 +399,17 @@ create_http301_response: ;rdi - pointer to buffer
get_request_type: ;rdi - pointer to buffer, ret = rax: request type
stackpush

cld
mov r10, -1
mov rsi, rdi
find_request_string:
inc r10
lodsb
cmp al, 0x20
jne find_request_string
mov rax, r10
add rax, 0x03
mov [request_offset], rax
cld
mov r10, -1
mov rsi, rdi
find_request_string:
inc r10
lodsb
cmp al, 0x20
jne find_request_string
mov rax, r10
add rax, 0x03
mov [request_offset], rax

mov rax, REQ_UNK

Expand Down
38 changes: 14 additions & 24 deletions main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
%include "constants.asm"
%include "macros.asm"

%define ASMTTPD_VERSION "0.4.2"

%define LISTEN_PORT 0x5000 ; PORT 80, network byte order
%define ASMTTPD_VERSION "0.4.4"

%define THREAD_COUNT 10 ; Number of worker threads

;Follwing amd64 syscall standards for internal function calls: rdi rsi rdx r10 r8 r9
section .data

section .data
%include "data.asm"

section .bss

%include "bss.asm"

section .text

%include "string.asm"
%include "http.asm"
%include "syscall.asm"
Expand All @@ -54,22 +50,17 @@ _start:
call print_line

mov rdi, [rsp] ;Num of args
cmp rdi,2 ;Exit if no argument, should be directory location
cmp rdi,3 ;Exit if no argument, should be directory location
jne exit_with_help

mov rdi, [rsp+16+8]; Port (second) parameter
call string_atoi
xchg al, ah
mov [listen_port], eax
mov rax, [rsp+16] ;Directory (first) parameter
mov [directory_path], rax

mov rdi, msg_using_directory
mov rsi, msg_using_directory_len
call sys_write
mov [directory_path], rax
mov rdi, [directory_path]
call get_string_length
mov rsi, rax
call print_line


; Register signal handlers ( just close all other threads by jumping to SYS_EXIT_GROUP )
mov r10, 8 ; sizeof(sigset_t) displays 128, but strace shows 8 ... so 8 it is! -_-
xor rdx, rdx
Expand All @@ -86,7 +77,6 @@ _start:
mov rax, SYS_RT_SIGACTION
syscall

;Try opening directory
mov rdi, [directory_path]
mov rdx, OPEN_DIRECTORY
Expand All @@ -105,7 +95,7 @@ _start:
mov rdi, [listen_socket]
call sys_reuse

;Bind to port 80
;Bind to port
call sys_bind_server
cmp rax, 0
jl exit_bind_error
Expand Down Expand Up @@ -193,8 +183,8 @@ worker_thread_continue:

mov rdi, [rbp-16]
call get_request_type
mov [request_type], rax
cmp BYTE [request_type], REQ_UNK
mov [request_type], rax
cmp BYTE [request_type], REQ_UNK
je worker_thread_400_response

;Find request
Expand Down
6 changes: 5 additions & 1 deletion syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ sys_listen:

sys_bind_server:
stackpush
mov rsi, [listen_port]
mov [sa + sin_port], rsi
mov rdi, [listen_socket]
mov rsi, sockaddr_in
mov rsi, sa
mov rdx, 16
mov rax, SYS_BIND
syscall
Expand Down

0 comments on commit 3bfa0dd

Please sign in to comment.