iglobal
  cMarkdownHeader file 'help_server/html_header.inc'
                  dd   0

  cMarkdownFooter file 'help_server/html_footer.inc'
                  dd   0
endg



proc ProcessFileRequest, .hSocket, .hFileName, .ptrPostData

  .md_result TMarkdownResults
  .ext       dd ?

begin
        pushad

        stdcall ConvertAliasString, '%FreshHelp%/'
        mov     ebx, eax

        stdcall StrDup, [.hFileName]
        stdcall StrURLDecode, eax
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax

; here, determine the type...

;        stdcall StrNormalizePath, ebx, '/\'
;        jc      .error404

        stdcall StrExtractExt, ebx
        jc      .error403
        push    eax

        xor     edx, edx

        stdcall StrPtr, eax
        cmp     [eax+string.len], 3
        jb      .ext_ok
        cmp     [eax+string.len], 5
        ja      .ext_ok

        mov     eax, [eax]
        or      eax, $20202000        ; lower case

        movx    edx, <'Content-type: text/html', 13, 10>

        cmp     eax, '.htm'
        je      .ext_ok

        cmp     eax, '.md '
        je      .ext_ok

        movx    edx, <'Content-type: text/plain', 13, 10>

        cmp     eax, '.txt'
        je      .ext_ok

        movx    edx, <'Content-type: text/css', 13, 10>

        cmp     eax, '.css'
        je      .ext_ok

        movx    edx, <'Content-type: image/png', 13, 10>

        cmp     eax, '.png'
        je      .ext_ok

        movx    edx, <'Content-type: image/svg+xml', 13, 10>

        cmp     eax, '.svg'
        je      .ext_ok

        movx    edx, <'Content-type: image/gif', 13, 10>

        cmp     eax, '.gif'
        je      .ext_ok

        movx    edx, <'Content-type: image/jpeg', 13, 10>

        cmp     eax, '.jpg'
        je      .ext_ok

        xor     edx, edx

.ext_ok:
        stdcall StrDel ; from the stack.
        test    edx, edx
        jz      .error403

        mov     [.ext], eax

        stdcall LoadBinaryFile, ebx
        jc      .error404

        mov     esi, eax

; now send the headers

        stdcall SendResponse, [.hSocket], headerHTTP200
        stdcall SocketSendStr, [.hSocket], edx
        stdcall SocketSendStr, [.hSocket], cCRLF

; check the file type:

        cmp     [.ext], '.md '
        jne     .send_static

; markdown formatted file.

        mov     edx, esi

; search the first cr/lf

.cr_loop:
        mov     al, [edx]
        inc     edx

        cmp     al, $0d
        je      .end_cr
        cmp     al, $0a
        je      .end_cr

        test    al, al
        jnz     .cr_loop
        jmp     .free

.end_cr:
        xor     al, $0d xor $0a
        cmp     [edx], al
        jne     @f
        inc     edx
@@:
        cmp     byte [edx], 0
        je      .free

        lea     eax, [.md_result]
        stdcall TranslateMarkdown, edx, 0, FALSE, eax

        stdcall SocketSendStr, [.hSocket], cMarkdownHeader
        stdcall SocketSendStr, [.hSocket], [.md_result.hContent]
        stdcall SocketSendStr, [.hSocket], cMarkdownFooter

        stdcall StrDel, [.md_result.hContent]
        stdcall StrDel, [.md_result.hIndex]
        stdcall StrDel, [.md_result.hKeywords]
        stdcall StrDel, [.md_result.hDescription]
        jmp     .free


.send_static:
        stdcall SocketSend, [.hSocket], esi, ecx, 0

.free:
        stdcall FreeMem, esi

.finish2:
        stdcall StrDel, ebx
        popad
        return

.error404:
        stdcall SendResponse, [.hSocket], headerHTTP404
        jmp     .finish2

.error403:
        stdcall SendResponse, [.hSocket], headerHTTP403
        jmp     .finish2

endp




