flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > let's build a LIBEVENT webserver without websocket + json

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
hi there,
how bout let's build a robust portable WSAAsyncSelect webserver together?

who wanna join, using win32ax.inc, make it as simple as possible then only we add other features.


2012-02-11 11:53PM
[ok] i think i fix the buffer for incoming socket, and buffer for holding x unit of client socket structure

[ok] i plan to do find CR,LF on incoming http response, write first line to log, then parse the incoming header data.

i plan to DOC_ROOT for serving file

i plan to test how it performs under multi connected user (eg, some downloading big file, some surfing pages)

i need to clear about connection:close, keep-alive, how to determine those states

i have question regarding before the RECV function, where windows put those data? what is the size of windows or winsock buffer for that part.

2012-02-13 09:54PM
next is using FindFirstFile to get file handle and return the file to requested client socket.

will try open several browser clients and download > 500mb files concurrently and hopefully our server functions normally.

will only check for several common mime file type based on extension.

will code HttpReplyBufferPrepare.

thank you.


Description: 2012-02-13 09:54PM
Download
Filename: webserver.zip
Filesize: 3.89 KB
Downloaded: 562 Time(s)



Last edited by sleepsleep on 09 Jul 2012, 11:01; edited 5 times in total
Post 09 Feb 2012, 07:13
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
How you will make portable web server, using Win32 API?
Post 09 Feb 2012, 07:27
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
portable in terms below,
- no installation
- double click to start running
- no registry writing/reading
- 1 config file, or config inside inc file, user fasm it to get the executable or we read it through ENV variables declared by users.

not target to run inside linux, bsd or other non-window architecture platform as of now.
Post 09 Feb 2012, 07:42
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Ah, I got it now.
But why not to make it portable in terms of OS as well?
Post 09 Feb 2012, 07:57
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
it needs more effort to start it first,

more and more more more effort to make it runs and everybody happy.

so, right now, i think the best way is, target window first, then we see how later.
Post 09 Feb 2012, 08:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17632
Location: In your JS exploiting you and your system
revolution
Let me know when you get around to adding the SSL code.

The rest of it, the general listener portion, is the easy part. And you can probably find a lot of asm code already written to do listening and basic page and/or application serving.
Post 09 Feb 2012, 11:39
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
no problem revolution, hope this project could reach that stage.

i will upload a basic one later and then let see how many fasmer interested to chip in ideas or better more efficient method.
Post 09 Feb 2012, 12:46
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
ok, a simple GUI that listen 80.

Code:
WM_SLSOCKET = WM_USER + 1

            .if [wmsg] = WM_SLSOCKET
                    mov     eax,[lparam]
                        and     eax,0xffff
          
                    .if eax = FD_CLOSE
                          invoke  closesocket,[wparam]
                                stdcall FileWriteLog,[logHandle],<'- FD_CLOSE',CR,LF>
                       .elseif eax = FD_ACCEPT
                             stdcall FileWriteLog,[logHandle],<'- FD_ACCEPT',CR,LF>
                      .elseif eax = FD_READ
                               stdcall FileWriteLog,[logHandle],<'- FD_READ',CR,LF>
                        .endif
              .elseif [wmsg] = WM_DESTROY
                 invoke  PostQuitMessage,0
                                   xor     eax,eax
                                     jmp .finish
         .endif    


you could use FileWriteLog for debug purpose,
next is accept client, i was thinking about virtualalloc to save array list accepted client socket handle

struct clientsockinfo
s dd 0 ; client socket
ssend dd 0 ; bytes sent, so max is 0xFFFF FFFF?
srecv dd 0 ; bytes received
buffer dd 0 ; memory address we alloc for socket through virtualalloc?
ends

if we want to prevent the ddos, then we need to record each connected ip, set a limit of connection within certain time-frame OR we should just leave this ddos job to router??

i read some MSDN page, it mentioned that we could just use send() and no need to handle FD_WRITE, so what are the pros & cons if we handle our FD_WRITE VS we should just invoke send and let window finish the job (assume we can't handle it better than them)?


Description:
Download
Filename: sleepserver.zip
Filesize: 2.13 KB
Downloaded: 496 Time(s)

Post 10 Feb 2012, 00:12
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
ok, i made a little bit progress,
now, it should capable to accept multiple client, up to
Code:
invoke listen,[wsListener],0xFF
SOCKETCLIENTINFO_POOL = 1000
invoke    VirtualAlloc,NULL,sizeof.SOCKET_CLIENTINFO * SOCKETCLIENTINFO_POOL,MEM_COMMIT,PAGE_READWRITE

struct SOCKET_CLIENTINFO
        ssock                   dd 0    ; the thing
 ssend                   dd 0
        srecv                   dd 0
        sbuff                   dd 0
ends
    


the WM_SLSOCKET modifed
Code:
.if eax = FD_CLOSE
    ;stdcall        FileWriteLog,[logHandle],<'- FD_CLOSE',CR,LF>
       invoke  closesocket,[wparam]
        stdcall SocketFree,[wparam]
.elseif eax = FD_ACCEPT
      ;stdcall        FileWriteLog,[logHandle],<'- FD_ACCEPT',CR,LF>
      invoke  accept,[wparam],NULL,NULL
                   inc     [memSocketClientCount]
                      push eax
    invoke  WSAAsyncSelect,eax,[hwnd],WM_SLSOCKET,FD_READ + FD_CLOSE
                    pop      eax
        stdcall SocketNew,eax
.elseif eax = FD_READ
      stdcall FileWriteLog,[logHandle],<'- FD_READ',CR,LF>
        stdcall SocketGetClient,[wparam]
    invoke  recv,[wparam],[eax + SOCKET_CLIENTINFO.sbuff],4095,0
        
    ; we do some process here then send() without doing FD_WRITE fttb
   invoke  send,[wparam],defaultResponse,<invoke lstrlen,defaultResponse>,0
      stdcall FileWriteLog,[logHandle],buffSocketClient
   stdcall FileWriteLog,[logHandle],<CR,LF>
.endif
    


Code:
proc SocketNew ssock
 stdcall FileWriteLog,[logHandle],<'- SocketNew',CR,LF>
      stdcall SocketGetClient,0
                   mov             edx,[ssock]
                 mov             [eax + SOCKET_CLIENTINFO.ssock],edx
                 mov             [eax + SOCKET_CLIENTINFO.sbuff],buffSocketClient
                    ret
endp

proc SocketGetClient val
 stdcall FileWriteLog,[logHandle],<'- SocketGetClient',CR,LF>
        mov     eax,[memSocketClientInfo]
   mov     edx,[val]
   .while DWORD [eax] <> edx
             add     eax,sizeof.SOCKET_CLIENTINFO
        .endw
       ret
endp

proc SocketFree ssock
    stdcall FileWriteLog,[logHandle],<'- SocketFree',CR,LF>
     stdcall SocketGetClient,[ssock]
                     mov     [eax + SOCKET_CLIENTINFO.sbuff],0
   ret
endp
    


and the defaultResponse is
Code:
db "HTTP/1.0 200 OK",13,10
db "Server : Gserver",13,10
db "Content-Type: text/html; charset=UTF-8",13,10
db "Content-Length: 13",13,10
db "Connection: close",13,10,13,10
db "<h1>Fasm</h1>",0
    
Post 10 Feb 2012, 08:24
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
next is how to parse the recv data http request data

is it CR,LF for windows browser, CR from linux or, CR,LF is universal?

i was thinking reading only up to 0xFF, if no CR,LF then, closesocket

is there any available Find String or Find CR LF function?

thanks.
Post 10 Feb 2012, 08:36
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
ok,
i manage to do a few updates.

i add this
Code:
SOCKETCLIENTINFO_POOL             = 1000
SOCKETCLIENT_HTTPBUFF         = 0xFFF
SOCKETCLIENT_HTTPBUFF_POOL   = 7
    

the HTTPBUFF defined the size of memory space for recv to use.

the HTTPBUFF_POOL * HTTBUFF is the total size we allocate under one time VirtualAlloc.

but there is a bug there, i haven't manage to find it

eg.
first few connection will return http response, but then failed for 1 or 2 connection, then ok back,

Sad


Last edited by sleepsleep on 11 Feb 2012, 16:01; edited 1 time in total
Post 11 Feb 2012, 10:27
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
ok, i think i nail the bug already, somewhere i forgot to put a RET in a proc...

so, i got a buffer space already for RECV, Smile
now is parsing the HTTP receive header, anyone want to chip in a PROC Smile

was thinking about using FASMLIB Smile since everything is written already in there i guess.
Post 11 Feb 2012, 14:20
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
i think i just always update the latest in the first post, instead of user scrolling hell to lower page...
Post 11 Feb 2012, 15:54
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
If you want to create serious project, it is good idea to use some version control system. I personally prefer fossil. You can host fossil repositories for free on http://chiselapp.com/
Post 11 Feb 2012, 17:03
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
asmhack



Joined: 01 Feb 2008
Posts: 431
asmhack
Code:
proc   FindCRLF lpBuffer

mov    eax, -1 
mov    esi, [lpBuffer]

test    esi, esi
jz      .notfound

.scan: 
inc    eax 

cmp    eax, SOCKETCLIENT_HTTPBUFF           
ja     .notfound 

cmp    word [esi + eax], 0x0a0d 
jnz    .scan 
ret

.notfound: 
mov    eax, -1 
ret

endp      ;;eax=length in bytes, else -1                  
    


Last edited by asmhack on 12 Feb 2012, 18:56; edited 1 time in total
Post 11 Feb 2012, 17:50
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
thanks asmhack, i will put it under webserver_string.asm Smile
Post 12 Feb 2012, 06:05
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
it seems that
Code:
cmp    word [esi + eax], 0xd0a
    

doesn't work
but
Code:
cmp        word [esi + eax],0x0a0d
    

works.
Post 12 Feb 2012, 18:04
View user's profile Send private message Reply with quote
asmhack



Joined: 01 Feb 2008
Posts: 431
asmhack
Thanks for labeling that out. I'll edit my post.
Post 12 Feb 2012, 18:54
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 9082
Location: ˛                             ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
we could parsing GET filename now, here is little bit of the snippet, it is highly inefficient asm code, so please try improve it by all means.

Code:
buffHttpRequestFilename       db 0xFFF dup (0)
stdcall     StringGetCrlf, [ssockbuff]
          cmp     eax,-1
              je      .noCRLF
             mov     edx,[ssockbuff]
             cmp     DWORD [edx],'GET '
                jne     .noGET
              add     edx,4
               cmp     BYTE [edx],'/'
            jne     .noGET
              cmp WORD [edx],'/ '
               je      .goGETDefault
               inc     edx
         
            mov     ecx,eax
             sub     ecx,5
               mov     ebx,buffHttpRequestFilename

             pusha
.copypad0:
             mov     al, byte [edx]
              mov     byte [ebx],al
               inc     edx
         inc ebx
             cmp     byte [edx],' '
            je      .pad0
               loop .copypad0
.pad0:
                popa
                mov     byte [ebx],0
    
Post 13 Feb 2012, 13:58
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Why did you go for the WSAAsyncSelect model?

It might be relatively simple and all, but it's very poor performance (doesn't matter until you need many clients and/or high throughput, but then you'll regret your choice), and there's lots of situations where you don't need a GUI for a server app.
Post 29 Feb 2012, 17:25
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.