Recent

Author Topic: http server. Get one line from http request  (Read 3608 times)

Anton Shevtsov

  • New Member
  • *
  • Posts: 13
http server. Get one line from http request
« on: December 20, 2017, 12:45:35 pm »
Hi,

I want simple tcp server. I need only one line of header i.e.

Quote
  GET /filename.ext?IP=1.2.3.4 HTTP/1.1

no body, no other lines.. only first line of http request
i must use synapse. What unit, methods i can used ?

p.s. simple examples are welcome ))

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: http server. Get one line from http request
« Reply #1 on: December 20, 2017, 12:54:52 pm »
no body, no other lines.. only first line of http request
Do you mean first line of the response or first line of the request ???

This is for the first line from the response:
Code: Pascal  [Select][+][-]
  1. uses httpsend;
  2. //...
  3. var
  4.   I: Integer;
  5.   HTTP: THTTPSend;
  6. begin
  7.   HTTP := THTTPSend.Create;
  8.   try
  9.     if HTTP.HTTPMethod('HEAD', your_url) then
  10.       if HTTP.Headers.Count > 0 then
  11.         Showmessage(HTTP.Headers.Strings[0]);
  12.   finally
  13.     HTTP.Free;
  14.   end;

Anton Shevtsov

  • New Member
  • *
  • Posts: 13
Re: http server. Get one line from http request
« Reply #2 on: December 20, 2017, 01:01:41 pm »
no body, no other lines.. only first line of http request
Do you mean first line of the response or first line of the request ???

This is for the first line from the response:
Code: Pascal  [Select][+][-]
  1. uses httpsend;
  2. //...
  3. var
  4.   I: Integer;
  5.   HTTP: THTTPSend;
  6. begin
  7.   HTTP := THTTPSend.Create;
  8.   try
  9.     if HTTP.HTTPMethod('HEAD', your_url) then
  10.       if HTTP.Headers.Count > 0 then
  11.         Showmessage(HTTP.Headers.Strings[0]);
  12.   finally
  13.     HTTP.Free;
  14.   end;

it's client side.. i need a server side (i want see clients requests)

client want download something from my server, and send request

Quote
GET /filename.ext?IP=1.2.3.4 HTTP/1.1
Host: 5.6.7.8:5222
User-Agent: blablabla
Accept: */*
Connection: Keep-Alive

i want
Quote
get:=foor.bar.getheaders[0]; // GET /filename.ext?IP=1.2.3.4 HTTP/1.1
and listen next request

p.s. my english is terrible )))

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: http server. Get one line from http request
« Reply #3 on: December 20, 2017, 01:07:33 pm »
it's client side.. i need a server side (i want see clients requests)
client want download something from my server, and send request
Ah, ok, I missed that (server) part.

Synapse doesn't have a ready made component for http-server.
But you can create one easily with just TTCPBlockSocket.

An example can be found here and you can simply adjust it like you want:
http://wiki.freepascal.org/Networking#Webserver_example

For your needs it's just sufficient to get the first  s := ASocket.RecvString(timeout); in AttendConnection in the example.
Although it's better to handle the complete sending of the response to the client. Otherwise the client might complain it doesn't receive anything back.

Note that the ListeningSocket is the one that listens to incoming http-connections.
It then passes the incoming socket to the ConnectionSocket which handles the receiving and sending of the actual data.

Normally, with a complete HTTP server you would want to handle multiple incoming connections at the same time and would need to handle the ConnectionSocket in a thread (so the ListeningSocket can go on listening to new incoming connection at the same time). But for a simple example this is not necessary.
« Last Edit: December 20, 2017, 01:09:30 pm by rvk »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: http server. Get one line from http request
« Reply #4 on: December 20, 2017, 02:10:43 pm »
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018