2009-10-16 11:18:05 +00:00
;routines for parsing a HTTP request
;to use - first call http_parse_request, then call http_get_value to get method name, path, and variable values
;NB - this routine uses the same buffer space and zero page locations as many other ip65 routines. so do not call
;other ip65 routines between the http_parse_request & http_get_value else odd things will happen.
.include " . . / inc/ c o m m o n . i "
.ifndef KPR_API_VERSION_NUMBER
.define EQU =
.include " . . / inc/ k i p p e r _ c o n s t a n t s . i "
.endif
.export http_parse_request
.export http_get_value
2009-10-18 10:37:03 +00:00
.export http_variables_buffer
2009-10-16 11:18:05 +00:00
.importzp copy_src
.importzp copy_dest
.import output_buffer
;reuse the copy_src zero page var
string_ p t r = c o p y _ s r c
table_ p t r =copy_dest
.bss
var_name : .res 1
hex_digit : .res 1
2009-10-18 10:37:03 +00:00
.data
http_variables_buffer : .word $ 2800 ;work area for storing variables extracted from query string
2009-10-16 11:18:05 +00:00
.code
;split a HTTP request into method (e.g. GET or POST), the path, and any querystring variables
;NB only the first letter in a variable name is significant. i.e. if a querystring contains variables 'a','alpha' & 'alabama', only the first one in will be retreivable.
;the method is stored in var $01
;the path is stored in var $02
;for example, parsing "GET /goober?a=foo&alpha=beta" would result in:
;value of A when calling http_get_value value returned by http_get_value
; #$01 "GET"
; #$02 "/goober"
; #'a' "foo"
; #'A' (error)
;inputs:
;AX = pointer to HTTP request
;outputs:
; none - but values can be retrieved through subsequent calls to http_get_value
http_parse_request :
stax s t r i n g _ p t r
2009-10-18 10:37:03 +00:00
ldax h t t p _ v a r i a b l e s _ b u f f e r
2009-10-16 11:18:05 +00:00
stax t a b l e _ p t r
lda #1 ;start of method
ldy #0
jsr p u t _ b y t e
lda ( s t r i n g _ p t r ) ,y
cmp #' / '
beq @gopher
jsr @check_end_of_string
bcs @gopher
lda ( s t r i n g _ p t r ) ,y
@extract_method:
cmp #' '
beq @end_of_method
jsr @check_end_of_string
bcc : +
jmp @done
:
jsr p u t _ b y t e
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jmp @extract_method
@gopher:
jsr @output_end_of_method
lda #' / '
jmp @got_path_char
@output_end_of_method:
lda #0 ;end of method
jsr p u t _ b y t e
lda #2 ;start of path
jmp p u t _ b y t e
@end_of_method:
jsr @output_end_of_method
@extract_path:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr @check_end_of_string
bcs @done
cmp #' ? '
beq @end_of_path
cmp #' & '
beq @end_of_path
@got_path_char:
jsr p u t _ b y t e
jmp @extract_path
@end_of_path:
lda #0 ;end of path
jsr p u t _ b y t e
@next_var:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr @check_end_of_string
bcs @done
jsr p u t _ b y t e
@skip_to_equals:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr @check_end_of_string
bcs @done
cmp #' ? '
beq @next_var
cmp #' & '
beq @next_var
cmp #' = '
beq @got_var
jmp @skip_to_equals
@got_var:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr @check_end_of_string
bcs @done
cmp #' ? '
beq @end_of_var
cmp #' & '
beq @end_of_var
cmp #' % '
beq @get_percent_encoded_byte
cmp #' + '
bne : +
lda #' '
:
@got_byte:
jsr p u t _ b y t e
jmp @got_var
@end_of_var:
lda #0
jsr p u t _ b y t e
jmp @next_var
@done:
lda #0
jsr p u t _ b y t e
jsr p u t _ b y t e
rts
@check_end_of_string:
cmp #0
beq @end_of_string
cmp #' '
beq @end_of_string
cmp #$ 0 a
beq @end_of_string
cmp #$ 0 d
beq @end_of_string
clc
rts
@end_of_string:
sec
rts
@get_percent_encoded_byte:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr p a r s e _ h e x _ d i g i t
asl
asl
asl
asl
sta h e x _ d i g i t
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
jsr p a r s e _ h e x _ d i g i t
clc
adc h e x _ d i g i t
jmp @got_byte
put_byte :
sta ( t a b l e _ p t r ) ,y
inc t a b l e _ p t r
bne : +
inc t a b l e _ p t r + 1
:
rts
;retrieve the value of a variable defined in the previously parsed HTTP request.
;inputs:
;A = variable to retrieve.
; to get the method (GET/POST/HEAD), pass A=$01.
; to get the path (everything between the method and the first '?'), pass A=$02.
;outputs:
; if variable exists in HTTP request, carry flag will be clear and AX points to value (null terminated string)
; if variable did not exist, carry flag will be set.
http_get_value :
sta v a r _ n a m e
2009-10-18 10:37:03 +00:00
ldax h t t p _ v a r i a b l e s _ b u f f e r
2009-10-16 11:18:05 +00:00
stax s t r i n g _ p t r
ldy #0
lda ( s t r i n g _ p t r ) ,y
@check_next_var:
beq @end_of_vars
cmp v a r _ n a m e
beq @got_var
;not the var we want, so skip over till next byte
@skip_till_null_byte:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
bne @skip_till_null_byte
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
bne @check_next_var
@end_of_vars:
sec
rts
@got_var:
jsr g e t _ n e x t _ b y t e _ i n _ s o u r c e
ldax s t r i n g _ p t r
clc
rts
get_next_byte_in_source :
inc s t r i n g _ p t r
bne : +
inc s t r i n g _ p t r + 1
:
lda ( s t r i n g _ p t r ) ,y
rts
parse_hex_digit :
cmp #$ 3 A
bcs @not_digit
sec
sbc #$ 30
rts
@not_digit:
ora #$ 20 ;make lower case
sec
sbc #' a ' - 1 0
rts