;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; FileName: WebHome.asm
; Author: Purple Endurer
; Functiion: Read a web homepage content
; DevEnv: Win XP SP2 + MASM32 v8
; log
; ------------------------------
; 2007-12-05 Added SearchStr() to search the webpage code end mark string
; 2007-12-04 Can read web homepage content
; 2007-12-03 Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.386
.model flat, stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include shell32.inc
includelib shell32.lib
include wsock32.inc
includelib wsock32.lib
WinMain PROTO :DWORD, :DWORD, :DWORD, :DWORD
btnShow_Click PROTO
fInitInternet proto :dword
fConnect proto lpszHostName:dword,nPortNumber:dword
SendHttpHead proto :LPSTR, :LPSTR
ReadSockData proto :LPSTR, :dword
ResizeConctrol proto :DWORD
SearchStr proto :LPSTR, :LPSTR
c_RichEditStyle equ WS_CHILD or WS_VISIBLE or ES_MULTILINE or WS_VSCROLL or ES_AUTOVSCROLL or WS_HSCROLL or ES_NOHIDESEL or ES_SAVESEL or ES_SELECTIONBAR
;sssssssssss
.data
;sssssssssss
g_szClsName label byte
g_szAppName db "HttpDemo", 0
if d_TestData eq 1
g_szTestURL db "bbs.pediy.com", 0
endif ;d_TestData
g_szFailIni db "Fail initialize internet connection!", 0
g_szFailGetHostName db "Fail to get host name!", 0
g_szConnect db "Fail to connect!", 0
g_szFailWSAStartup db "Fail to WSAStartup", 0
g_szEnterURL db "请先输入URL!", 0
g_szEditCls db "EDIT", 0
g_szBtnCls db "button", 0
g_szNoRichEdit db "无法载入"
g_szRichEditDLL db "RICHED20.DLL", 0
g_szRichEditClass db "RichEdit20A", 0
g_szBtnReadText db "&R 读取", 0
g_szFmt1 db "GET /%s HTTP/1.1", 0dh, 0ah
db "Host:%s", 0dh, 0ah
db "Accept: */*", 0dh, 0ah
db "User-Agent: Mozilla/4.0"
db "(compatible; MSIE 6.00; Windows 2000)", 0dh, 0ah
db "Connection:Keep-Alive", 0dh, 0ah
db 0dh, 0ah
g_szCR db 0dh, 0ah, 0
g_szHTTP400 db "HTTP/1.1 400 Bad Request", 0
g_szFmt2 db "WSACleanup failed with error %d", 0
btnShow_Click proc
;--- First I initialize the internet and get the socket using this code.
invoke fInitInternet, g_hWndMain
test eax, eax
.if !ZERO?
invoke MessageBox, g_hWndMain, eax, addr g_szAppName, 0
.else
;--- Second, I connect the socket using this code
invoke fConnect, addr g_szURL, 80
test eax, eax
.if !ZERO?
invoke MessageBox, g_hWndMain, eax, addr g_szAppName, 0
.else
invoke SendHttpHead, addr g_szURL, NULL
mov eax, [eax] ; copy the pointer to the actual IP address into eax
mov eax, [eax] ; copy IP address into eax
mov sin.sin_addr.S_un.S_addr, eax ;mov sin.sin_addr, eax
ReadSockData proc lpszBuffer: LPSTR, dwMax_buf_len: dword
invoke RtlZeroMemory, lpszBuffer, dwMax_buf_len
mov edi, lpszBuffer
mov esi, dwMax_buf_len
dec esi
@@:
push esi
push edi
invoke recv, sock, edi, esi, 0
pop edi
pop esi
add edi, eax
sub esi, eax
jz @get_http_pageRet
;pushad
;invoke MessageBox, g_hWndMain, addr g_szAppName, addr g_szAppName, 0
;popad
cmp eax, 0
jg @B
@get_http_pageRet:
ret
ReadSockData endp
; if eax=-1, no found
; else eax = sub string position
SearchStr proc lpszOrgStr: LPSTR, lpszSubStr: LPSTR
local dwPos: dword
mov edi, lpszSubStr
cmp byte ptr [edi], 0
je @NoFound
mov esi, lpszOrgStr
mov dwPos, esi
@SearchStrLoop1Begin:
mov al, byte ptr [esi]
test al, al
jz @NoFound
cmp al, byte ptr [edi]
jne @SearchStrLoop1Next
@SearchStrLoop2Begin:
inc esi
inc edi
mov ah, byte ptr [edi]
test ah, ah
jz @Found
mov al, byte ptr [esi]
test al, al
jz @NoFound
cmp al, ah
je @SearchStrLoop2Begin
mov edi, lpszSubStr
@SearchStrLoop1Next:
inc dwPos
mov esi, dwPos
jmp @SearchStrLoop1Begin
@NoFound:
xor eax, eax ;mov eax, -1
dec eax
jmp @SearchStrRet
@Found:
mov eax, dwPos
sub eax, lpszOrgStr
@SearchStrRet:
ret
SearchStr endp
if c_Resize eq 1
ResizeConctrol PROC wh:DWORD
;--- Get main window width
mov eax, wh
mov ecx, eax
movzx eax, ax ; width
push eax ;push for resizing the ver info edit
sub eax, 4
shr ecx, 16 ; height
sub ecx, c_EditURLHeight+10
;--- Resize the Get button
pop eax
sub eax, 5+c_BtnReadWidth
push eax
invoke MoveWindow, g_hBtnRead, eax, c_BtnReadTop, c_BtnReadWidth, c_BtnReadHeight, TRUE
;--- Resize the file spec editbox
pop eax
sub eax, 10
invoke MoveWindow, g_hEditURL, c_EditURLLeft, c_EditURLTop, eax, c_EditURLHeight, TRUE