Subversion Repositories Kolibri OS

Rev

Rev 4162 | Rev 4168 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4162 Rev 4167
Line 49... Line 49...
49
        stosb
49
        stosb
50
        jmp     @r
50
        jmp     @r
51
  @@:
51
  @@:
52
}
52
}
Line -... Line 53...
-
 
53
 
-
 
54
macro HTTP_init_buffer buffer, socketnum {
-
 
55
 
-
 
56
        mov     eax, buffer
-
 
57
        push    socketnum
-
 
58
        popd    [eax + http_msg.socket]
-
 
59
        lea     esi, [eax + http_msg.data]
-
 
60
        mov     [eax + http_msg.flags], 0
-
 
61
        mov     [eax + http_msg.write_ptr], esi
-
 
62
        mov     [eax + http_msg.buffer_length], BUFFERSIZE -  http_msg.data
-
 
63
        mov     [eax + http_msg.chunk_ptr], 0
-
 
64
 
-
 
65
        mov     [eax + http_msg.status], 0
-
 
66
        mov     [eax + http_msg.header_length], 0
-
 
67
        mov     [eax + http_msg.content_length], 0
-
 
68
}
53
 
69
 
Line 54... Line 70...
54
section '.flat' code readable align 16
70
section '.flat' code readable align 16
55
 
71
 
56
;;===========================================================================;;
72
;;===========================================================================;;
Line 106... Line 122...
106
        hostname        dd ?
122
        hostname        dd ?
107
        pageaddr        dd ?
123
        pageaddr        dd ?
108
        sockaddr        dd ?
124
        sockaddr        dd ?
109
        socketnum       dd ?
125
        socketnum       dd ?
110
        buffer          dd ?
126
        buffer          dd ?
-
 
127
        port            dd ?
111
endl
128
endl
Line 112... Line 129...
112
 
129
 
113
; split the URL into hostname and pageaddr
130
; split the URL into hostname and pageaddr
114
        stdcall parse_url, [URL]
131
        stdcall parse_url, [URL]
Line 119... Line 136...
119
 
136
 
120
; Do we need to use a proxy?
137
; Do we need to use a proxy?
121
        cmp     [proxyAddr], 0
138
        cmp     [proxyAddr], 0
Line 122... Line -...
122
        jne     .proxy_done
-
 
123
 
139
        jne     .proxy_done
Line 124... Line 140...
124
        ; TODO
140
 
125
  .proxy_done:
-
 
126
 
141
  .proxy_done:
127
; Resolve the hostname
-
 
128
        DEBUGF  1, "Resolving hostname\n"
-
 
129
        push    esp     ; reserve stack place
-
 
-
 
142
 
130
        push    esp     ; fourth parameter
143
;;;;
131
        push    0       ; third parameter
144
        mov     [port], 80      ;;;; FIXME
132
        push    0       ; second parameter
-
 
133
        push    [hostname]
145
 
134
        call    [getaddrinfo]
146
; Connect to the other side.
-
 
147
        stdcall open_connection, [hostname], [port]
Line 135... Line -...
135
        pop     esi
-
 
136
        test    eax, eax
-
 
137
        jnz     .error
148
        test    eax, eax
138
 
149
        jz      .error
139
; getaddrinfo returns addrinfo struct, make the pointer to sockaddr struct
150
        mov     [socketnum], eax
140
        mov     esi, [esi + addrinfo.ai_addr]
151
 
-
 
152
; Create the HTTP request.
-
 
153
        invoke  mem.alloc, BUFFERSIZE
-
 
154
        test    eax, eax
Line 141... Line 155...
141
        mov     [sockaddr], esi
155
        jz      .error
142
        mov     eax, [esi + sockaddr_in.sin_addr]
156
        mov     [buffer], eax
143
        test    eax, eax
-
 
Line 144... Line 157...
144
        jz      .error
157
        mov     edi, eax
145
 
158
        DEBUGF  1, "Buffer has been allocated.\n"
Line -... Line 159...
-
 
159
 
-
 
160
        mov     esi, str_get
-
 
161
        copy_till_zero
-
 
162
 
-
 
163
        mov     esi, [pageaddr]
-
 
164
        copy_till_zero
-
 
165
 
-
 
166
        mov     esi, str_http11
-
 
167
        mov     ecx, str_http11.length
-
 
168
        rep     movsb
-
 
169
 
-
 
170
        mov     esi, [hostname]
-
 
171
        copy_till_zero
-
 
172
 
146
        DEBUGF  1, "Server ip=%u.%u.%u.%u\n", \
173
        mov     esi, str_close
-
 
174
        mov     ecx, str_close.length
-
 
175
        rep     movsb
-
 
176
 
-
 
177
        mov     byte[edi], 0
147
        [esi + sockaddr_in.sin_addr]:1, [esi + sockaddr_in.sin_addr + 1]:1, \
178
        DEBUGF  1, "Request:\n%s", [buffer]
148
        [esi + sockaddr_in.sin_addr + 2]:1, [esi + sockaddr_in.sin_addr + 3]:1
179
 
149
 
180
; Send the request
150
        mov     [esi + sockaddr_in.sin_family], AF_INET4
-
 
151
        mov     [esi + sockaddr_in.sin_port], 80 shl 8  ;;; FIXME
181
        mov     esi, edi
Line 152... Line 182...
152
 
182
        sub     esi, [buffer]   ; length
-
 
183
        xor     edi, edi        ; flags
-
 
184
 
-
 
185
        mcall   send, [socketnum], [buffer]
-
 
186
        test    eax, eax
-
 
187
        jz      .error
-
 
188
        DEBUGF  1, "Request has been sent to server.\n"
-
 
189
 
-
 
190
        HTTP_init_buffer [buffer], [socketnum]
-
 
191
 
-
 
192
        ret                     ; return buffer ptr
-
 
193
 
-
 
194
  .error:
-
 
195
        DEBUGF  1, "Error!\n"
-
 
196
        xor     eax, eax        ; return 0 = error
-
 
197
        ret
-
 
198
 
-
 
199
endp
-
 
200
 
-
 
201
 
-
 
202
 
-
 
203
;;================================================================================================;;
-
 
204
proc HTTP_head URL ;///////////////////////////////////////////////////////////////////////////////;;
-
 
205
;;------------------------------------------------------------------------------------------------;;
-
 
206
;?                                                                                                ;;
-
 
207
;;------------------------------------------------------------------------------------------------;;
-
 
208
;> _                                                                                              ;;
-
 
209
;;------------------------------------------------------------------------------------------------;;
-
 
210
;< eax = 0 (error) / buffer ptr                                                                   ;;
-
 
211
;;================================================================================================;;
-
 
212
locals
-
 
213
        hostname        dd ?
-
 
214
        pageaddr        dd ?
153
; Connect to the server.
215
        sockaddr        dd ?
154
        mcall   socket, AF_INET4, SOCK_STREAM, 0
216
        socketnum       dd ?
155
        test    eax, eax
217
        buffer          dd ?
-
 
218
        port            dd ?
-
 
219
endl
-
 
220
 
-
 
221
; split the URL into hostname and pageaddr
-
 
222
        stdcall parse_url, [URL]
Line 156... Line 223...
156
        jz      .error
223
        test    eax, eax
-
 
224
        jz      .error
-
 
225
        mov     [hostname], eax
-
 
226
        mov     [pageaddr], ebx
-
 
227
 
-
 
228
; Do we need to use a proxy?
-
 
229
        cmp     [proxyAddr], 0
-
 
230
        jne     .proxy_done
-
 
231
 
-
 
232
        ; TODO: set hostname to that of the
-
 
233
  .proxy_done:
Line 157... Line 234...
157
        mov     [socketnum], eax
234
 
158
        DEBUGF  1, "Socket: 0x%x\n", eax
235
;;;;
159
 
236
        mov     [port], 80      ;;;; FIXME
160
        mcall   connect, [socketnum], [sockaddr], 18
237
 
161
        test    eax, eax
238
; Connect to the other side.
-
 
239
        stdcall open_connection, [hostname], [port]
162
        jnz     .error
240
        test    eax, eax
Line 163... Line 241...
163
        DEBUGF  1, "Socket is now connected.\n"
241
        jz      .error
164
 
242
        mov     [socketnum], eax
-
 
243
 
165
        ; TODO: free address buffer(s)
244
; Create the HTTP request.
166
 
245
        invoke  mem.alloc, BUFFERSIZE
Line 167... Line 246...
167
; Create the HTTP request.
246
        test    eax, eax
168
        invoke  mem.alloc, BUFFERSIZE
247
        jz      .error
169
        test    eax, eax
248
        mov     [buffer], eax
Line 188... Line 267...
188
        rep     movsb
267
        rep     movsb
Line 189... Line 268...
189
 
268
 
190
        mov     byte[edi], 0
269
        mov     byte[edi], 0
Line 191... Line 270...
191
        DEBUGF  1, "Request:\n%s", [buffer]
270
        DEBUGF  1, "Request:\n%s", [buffer]
192
 
271
 
193
; now send the request
272
; Send the request
194
        mov     esi, edi
273
        mov     esi, edi
Line 195... Line 274...
195
        sub     esi, [buffer]   ; length
274
        sub     esi, [buffer]   ; length
196
        xor     edi, edi        ; flags
275
        xor     edi, edi        ; flags
197
 
276
 
198
        mcall   send, [socketnum], [buffer]
277
        mcall   send, [socketnum], [buffer]
Line 199... Line -...
199
        test    eax, eax
-
 
200
        jz      .error
-
 
201
        DEBUGF  1, "Request has been sent to server.\n"
278
        test    eax, eax
202
 
-
 
203
; Now that we have sent the request, re-purpose buffer as receive buffer
-
 
204
        mov     eax, [buffer]
-
 
205
        push    [socketnum]
-
 
206
        popd    [eax + http_msg.socket]
-
 
207
        lea     esi, [eax + http_msg.data]
-
 
Line -... Line 279...
-
 
279
        jz      .error
-
 
280
        DEBUGF  1, "Request has been sent to server.\n"
-
 
281
 
-
 
282
        HTTP_init_buffer [buffer], [socketnum]
-
 
283
 
-
 
284
        ret                     ; return buffer ptr
-
 
285
 
-
 
286
  .error:
-
 
287
        DEBUGF  1, "Error!\n"
-
 
288
        xor     eax, eax        ; return 0 = error
-
 
289
        ret
-
 
290
 
-
 
291
endp
-
 
292
 
-
 
293
 
-
 
294
;;================================================================================================;;
-
 
295
proc HTTP_post URL, content, content_type, content_length ;///////////////////////////////////////;;
-
 
296
;;------------------------------------------------------------------------------------------------;;
-
 
297
;?                                                                                                ;;
-
 
298
;;------------------------------------------------------------------------------------------------;;
-
 
299
;> _                                                                                              ;;
-
 
300
;;------------------------------------------------------------------------------------------------;;
-
 
301
;< eax = 0 (error) / buffer ptr                                                                   ;;
-
 
302
;;================================================================================================;;
-
 
303
locals
-
 
304
        hostname        dd ?
-
 
305
        pageaddr        dd ?
-
 
306
        sockaddr        dd ?
-
 
307
        socketnum       dd ?
-
 
308
        buffer          dd ?
-
 
309
        port            dd ?
-
 
310
endl
-
 
311
 
-
 
312
; split the URL into hostname and pageaddr
-
 
313
        stdcall parse_url, [URL]
-
 
314
        test    eax, eax
-
 
315
        jz      .error
-
 
316
        mov     [hostname], eax
-
 
317
        mov     [pageaddr], ebx
-
 
318
 
-
 
319
; Do we need to use a proxy?
-
 
320
        cmp     [proxyAddr], 0
-
 
321
        jne     .proxy_done
-
 
322
 
-
 
323
        ; TODO: set hostname to that of the
-
 
324
  .proxy_done:
-
 
325
 
-
 
326
;;;;
-
 
327
        mov     [port], 80      ;;;; FIXME
-
 
328
 
-
 
329
; Connect to the other side.
-
 
330
        stdcall open_connection, [hostname], [port]
-
 
331
        test    eax, eax
-
 
332
        jz      .error
-
 
333
        mov     [socketnum], eax
-
 
334
 
-
 
335
; Create the HTTP request.
-
 
336
        invoke  mem.alloc, BUFFERSIZE
-
 
337
        test    eax, eax
-
 
338
        jz      .error
-
 
339
        mov     [buffer], eax
-
 
340
        mov     edi, eax
-
 
341
        DEBUGF  1, "Buffer has been allocated.\n"
-
 
342
 
-
 
343
        mov     esi, str_post
-
 
344
        copy_till_zero
208
        mov     [eax + http_msg.flags], 0
345
 
-
 
346
        mov     esi, [pageaddr]
-
 
347
        copy_till_zero
-
 
348
 
-
 
349
        mov     esi, str_http11
-
 
350
        mov     ecx, str_http11.length
-
 
351
        rep     movsb
209
        mov     [eax + http_msg.write_ptr], esi
352
 
-
 
353
        mov     esi, [hostname]
-
 
354
        copy_till_zero
210
        mov     [eax + http_msg.buffer_length], BUFFERSIZE -  http_msg.data
355
 
-
 
356
        mov     esi, str_post_cl
-
 
357
        mov     ecx, str_post_cl.length
-
 
358
        rep     movsb
-
 
359
 
-
 
360
        mov     eax, [content_length]
-
 
361
        call    ascii_dec
-
 
362
 
-
 
363
        mov     esi, str_post_ct
-
 
364
        mov     ecx, str_post_ct.length
-
 
365
        rep     movsb
-
 
366
 
-
 
367
        mov     esi, [content_type]
-
 
368
        rep     movsb
-
 
369
 
-
 
370
        mov     esi, str_close
-
 
371
        mov     ecx, str_close.length
-
 
372
        rep     movsb
-
 
373
 
-
 
374
        mov     byte[edi], 0
-
 
375
        DEBUGF  1, "Request:\n%s", [buffer]
-
 
376
 
-
 
377
; Send the request
-
 
378
        mov     esi, edi
-
 
379
        sub     esi, [buffer]   ; length
-
 
380
        xor     edi, edi        ; flags
-
 
381
        mcall   send, [socketnum], [buffer]
-
 
382
        test    eax, eax
-
 
383
        jz      .error
-
 
384
        DEBUGF  1, "Request has been sent to server.\n"
-
 
385
 
-
 
386
        mcall   send, [socketnum], [content], [content_length]
-
 
387
        test    eax, eax
Line 211... Line 388...
211
        mov     [eax + http_msg.chunk_ptr], 0
388
        jz      .error
Line 212... Line 389...
212
 
389
        DEBUGF  1, "Data has been sent to server.\n"
213
        mov     [eax + http_msg.status], 0
390
 
Line 503... Line 680...
503
        mov     eax, [ebp + http_msg.chunk_ptr]
680
        mov     eax, [ebp + http_msg.chunk_ptr]
504
        sub     eax, [ebp + http_msg.header_length]
681
        sub     eax, [ebp + http_msg.header_length]
505
        sub     eax, http_msg.data
682
        sub     eax, http_msg.data
506
        sub     eax, ebp
683
        sub     eax, ebp
507
        mov     [ebp + http_msg.content_length], eax
684
        mov     [ebp + http_msg.content_length], eax
508
 
-
 
509
  .got_all_data:
685
  .got_all_data:
510
        DEBUGF  1, "We got all the data! (%u bytes)\n", [ebp + http_msg.content_length]
686
        DEBUGF  1, "We got all the data! (%u bytes)\n", [ebp + http_msg.content_length]
511
        or      [ebp + http_msg.flags], FLAG_GOT_DATA
687
        or      [ebp + http_msg.flags], FLAG_GOT_DATA
512
        mcall   close, [ebp + http_msg.socket]
688
        mcall   close, [ebp + http_msg.socket]
513
        popa
689
        popa
Line 609... Line 785...
609
endp
785
endp
Line 610... Line 786...
610
 
786
 
Line -... Line 787...
-
 
787
 
-
 
788
; internal procedures start here:
-
 
789
 
-
 
790
;;================================================================================================;;
-
 
791
proc open_connection hostname, port ;/////////////////////////////////////////////////////////////;;
-
 
792
;;------------------------------------------------------------------------------------------------;;
-
 
793
;?                                                                                                ;;
-
 
794
;;------------------------------------------------------------------------------------------------;;
-
 
795
;> _                                                                                              ;;
-
 
796
;;------------------------------------------------------------------------------------------------;;
-
 
797
;< eax = -1 (error) / 0                                                                           ;;
-
 
798
;;================================================================================================;;
-
 
799
 
-
 
800
locals
-
 
801
        sockaddr        dd ?
-
 
802
        socketnum       dd ?
-
 
803
endl
-
 
804
 
-
 
805
; Resolve the hostname
-
 
806
        DEBUGF  1, "Resolving hostname\n"
-
 
807
        push    esp     ; reserve stack place
-
 
808
        push    esp     ; fourth parameter
-
 
809
        push    0       ; third parameter
-
 
810
        push    0       ; second parameter
-
 
811
        push    [hostname]
-
 
812
        call    [getaddrinfo]
-
 
813
        pop     esi
-
 
814
        test    eax, eax
-
 
815
        jnz     .error1
-
 
816
 
-
 
817
; getaddrinfo returns addrinfo struct, make the pointer to sockaddr struct
-
 
818
        mov     esi, [esi + addrinfo.ai_addr]
-
 
819
        mov     [sockaddr], esi
-
 
820
        mov     eax, [esi + sockaddr_in.sin_addr]
-
 
821
        test    eax, eax
-
 
822
        jz      .error2
-
 
823
 
-
 
824
        DEBUGF  1, "Server ip=%u.%u.%u.%u\n", \
-
 
825
        [esi + sockaddr_in.sin_addr]:1, [esi + sockaddr_in.sin_addr + 1]:1, \
-
 
826
        [esi + sockaddr_in.sin_addr + 2]:1, [esi + sockaddr_in.sin_addr + 3]:1
-
 
827
 
-
 
828
        mov     [esi + sockaddr_in.sin_family], AF_INET4
-
 
829
        mov     eax, [port]
-
 
830
        xchg    al, ah
-
 
831
        mov     [esi + sockaddr_in.sin_port], ax
-
 
832
 
-
 
833
; Connect to the server.
-
 
834
        mcall   socket, AF_INET4, SOCK_STREAM, 0
-
 
835
        test    eax, eax
-
 
836
        jz      .error2
-
 
837
        mov     [socketnum], eax
-
 
838
        DEBUGF  1, "Socket: 0x%x\n", eax
-
 
839
 
-
 
840
        mcall   connect, [socketnum], [sockaddr], 18
-
 
841
        test    eax, eax
-
 
842
        jnz     .error2
-
 
843
        DEBUGF  1, "Socket is now connected.\n"
-
 
844
 
-
 
845
; free allocated memory
-
 
846
        push    [sockaddr]
-
 
847
        call    [freeaddrinfo]
-
 
848
 
-
 
849
        mov     eax, [socketnum]
-
 
850
        ret
-
 
851
 
-
 
852
  .error2:
-
 
853
 
-
 
854
; free allocated memory
-
 
855
        push    [sockaddr]
-
 
856
        call    [freeaddrinfo]
-
 
857
 
-
 
858
  .error1:
-
 
859
        xor     eax, eax
-
 
860
        ret
Line 611... Line 861...
611
 
861
 
612
; internal procedures start here:
862
endp
613
 
863
 
614
 
864
 
Line 714... Line 964...
714
        ret
964
        ret
Line 715... Line 965...
715
 
965
 
Line -... Line 966...
-
 
966
endp
-
 
967
 
-
 
968
 
-
 
969
; in: eax = number
-
 
970
;     edi = ptr where to store ascii
-
 
971
ascii_dec:
-
 
972
 
-
 
973
        mov     ecx, 10
-
 
974
  .loop:
-
 
975
        xor     edx, edx
-
 
976
        div     ecx
-
 
977
        add     dl, '0'
-
 
978
        mov     byte[edi], dl
Line -... Line 979...
-
 
979
        inc     edi
Line 716... Line 980...
716
endp
980
        test    eax, eax
717
 
981
        jnz     .loop
718
 
982
 
Line 757... Line 1021...
757
@EXPORT:
1021
@EXPORT:
758
export  \
1022
export  \
759
        lib_init                , 'lib_init'            , \
1023
        lib_init                , 'lib_init'            , \
760
        0x00010001              , 'version'             , \
1024
        0x00010001              , 'version'             , \
761
        HTTP_get                , 'get'                 , \
1025
        HTTP_get                , 'get'                 , \
-
 
1026
        HTTP_head               , 'head'                , \
-
 
1027
        HTTP_post               , 'post'                , \
762
        find_header_field       , 'find_header_field'   , \
1028
        find_header_field       , 'find_header_field'   , \
763
        HTTP_process            , 'process'
1029
        HTTP_process            , 'process'
Line 764... Line -...
764
 
-
 
765
;        HTTP_head               , 'head'                , \
-
 
766
;        HTTP_post               , 'post'                , \
1030
 
767
;        HTTP_put                , 'put'                 , \
1031
;        HTTP_put                , 'put'                 , \
768
;        HTTP_delete             , 'delete'              , \
1032
;        HTTP_delete             , 'delete'              , \
769
;        HTTP_trace              , 'trace'               , \
1033
;        HTTP_trace              , 'trace'               , \
Line 781... Line 1045...
781
key_user        db 'user', 0
1045
key_user        db 'user', 0
782
key_password    db 'password', 0
1046
key_password    db 'password', 0
Line 783... Line 1047...
783
 
1047
 
784
str_http11      db ' HTTP/1.1', 13, 10, 'Host: '
1048
str_http11      db ' HTTP/1.1', 13, 10, 'Host: '
-
 
1049
  .length       = $ - str_http11
-
 
1050
str_post_cl     db 13, 10, 'Content-Length: '
-
 
1051
  .length       = $ - str_post_cl
-
 
1052
str_post_ct     db 13, 10, 'Content-Type: '
785
  .length       = $ - str_http11
1053
  .length       = $ - str_post_ct
786
str_close       db 13, 10, 'User-Agent: KolibriOS libHTTP/1.0', 13, 10, 'Connection: Close', 13, 10, 13, 10
1054
str_close       db 13, 10, 'User-Agent: KolibriOS libHTTP/1.0', 13, 10, 'Connection: Close', 13, 10, 13, 10
787
  .length       = $ - str_close
1055
  .length       = $ - str_close
788
str_proxy_auth  db 13, 10, 'Proxy-Authorization: Basic '
1056
str_proxy_auth  db 13, 10, 'Proxy-Authorization: Basic '
Line 792... Line 1060...
792
                db '0123456789+/'
1060
                db '0123456789+/'
Line 793... Line 1061...
793
 
1061
 
794
str_cl          db 'content-length', 0
1062
str_cl          db 'content-length', 0
795
str_slash       db '/', 0
1063
str_slash       db '/', 0
-
 
1064
str_te          db 'transfer-encoding', 0
-
 
1065
str_get         db 'GET ', 0
-
 
1066
str_head        db 'HEAD ', 0
Line 796... Line 1067...
796
str_te          db 'transfer-encoding', 0
1067
str_post        db 'POST ', 0
Line 797... Line 1068...
797
 
1068
 
798
include_debug_strings
1069
include_debug_strings