.include "../inc/common.inc"
.include "../inc/commonprint.inc"
.include "../inc/net.inc"

.export start

.import exit_to_basic

.import print_a
.import get_key
.import ascii_to_native

.import http_parse_request
.import http_get_value


; keep LD65 happy
.segment "INIT"
.segment "ONCE"


.segment "STARTUP"

  ; switch to lower case charset
  lda #14
  jsr print_a

start:
  jsr print_cr
  ldax #query_1
  jsr test_querystring
  ldax #query_2
  jsr test_querystring
  ldax #query_3
  jsr test_querystring
  jsr get_key
  ldax #query_4
  jsr test_querystring
  ldax #query_5
  jsr test_querystring
  ldax #query_6
  jsr test_querystring
  jmp exit_to_basic

test_querystring:
  stax temp_ax
  jsr print_ascii_as_native
  jsr print_cr
  ldax temp_ax
  jsr http_parse_request

  lda #1
  jsr print_var
  lda #2
  jsr print_var
  lda #'h'
  jsr print_var
  lda #'m'
  jsr print_var
  lda #'q'
  jsr print_var
  rts

print_var:
  pha
  cmp #1
  beq @print_method
  cmp #2
  beq @print_path
  jsr ascii_to_native

  jsr print_a
@print_equals:
  lda #'='
  jsr print_a
  pla
  jsr http_get_value
  bcc @found_var_value
  lda #'?'
  jsr print_a
  jmp print_cr
@found_var_value:
  jsr print_ascii_as_native
  jmp print_cr

@print_path:
  ldax #path
  jmp @print_caption
@print_method:
  ldax #method
@print_caption:
  jsr print_ascii_as_native
  jmp @print_equals


.bss

temp_ax: .res 2


.rodata

path:
  .byte "path",0
method:
  .byte "method",0

query_1:
  .byte "GET /?h=slack&m=goober+woober+woo%21+%3B+i+am+text HTTP/1.1",0
query_2:
  .byte "POST /?h=slack&m=goober+woober+woo!+%3b+i+am+text",0
query_3:
  .byte "GET /?handle=slack&message=goober+woober+woo%21+%3B+i+am+text+%0d%0a%21%40%23%24%25%5E%26%25%5D%5B%7B%7D& HTTP/1.1",0
query_4:
  .byte "GET /this/is/a/long/q/path.html?q=foo",0
query_5:
  .byte "/this/is/a/gopher_selector",0
query_6:
  .byte $0d,$0a,0               ; this should also be a gopher path



; -- LICENSE FOR parsequerystring.s --
; The contents of this file are subject to the Mozilla Public License
; Version 1.1 (the "License"); you may not use this file except in
; compliance with the License. You may obtain a copy of the License at
; http://www.mozilla.org/MPL/
;
; Software distributed under the License is distributed on an "AS IS"
; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
; License for the specific language governing rights and limitations
; under the License.
;
; The Original Code is ip65.
;
; The Initial Developer of the Original Code is Jonno Downes,
; jonno@jamtronix.com.
; Portions created by the Initial Developer are Copyright (C) 2009
; Jonno Downes. All Rights Reserved.
; -- LICENSE END --
