⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webserv1.cpp

📁 文件名称:新曦 我的资源 搜索软件 源程序(Borland Delphi 7)说明
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    DisplayMemo->Lines->Add(Msg);
    DisplayMemo->Lines->EndUpdate();
    // Makes last line visible
    SendMessage(DisplayMemo->Handle, EM_SCROLLCARET, 0, 0);
}
//---------------------------------------------------------------------------
// This event handler is called when user clicks on start button. It is also
// called from FormShow event handler, at program startup. It starts server.
// We need to pass default document, document directory and client class
// to HTTP server component. Client class is very usefull because it
// instruct server component to instanciate our own client class instead of
// defualt client class. Using our own client class will enables you to add
// any data we need to handle our application. This data is private for each
// client.
// When server is started, we will get OnServerStarted event triggered.
void __fastcall TAppBaseForm::StartButtonClick(TObject *Sender)
{
    HttpServer1->DocDir      = Trim(DocDirEdit->Text);
    HttpServer1->DefaultDoc  = Trim(DefaultDocEdit->Text);
    HttpServer1->Port        = Trim(PortEdit->Text);
    HttpServer1->ClientClass = __classid(TMyHttpConnection);
    HttpServer1->Start();
}
//---------------------------------------------------------------------------
// This event handler is triggered when user clicks on stop button. We just
// stop the server. We will get OnServerStopped event triggered.
void __fastcall TAppBaseForm::StopButtonClick(TObject *Sender)
{
    HttpServer1->Stop();
}
//---------------------------------------------------------------------------
// This event handler is triggered when user clicks on clear buttoN; We just
// clear the memo used for displaying activity.
void __fastcall TAppBaseForm::ClearButtonClick(TObject *Sender)
{
    DisplayMemo->Clear();
}
//---------------------------------------------------------------------------
// This event handler is triggered when HTTP server is started, that is when
// server socket has started listening.
void __fastcall TAppBaseForm::HttpServer1ServerStarted(TObject *Sender)
{
    DocDirEdit->Enabled     = FALSE;
    DefaultDocEdit->Enabled = FALSE;
    PortEdit->Enabled       = FALSE;
    StartButton->Enabled    = FALSE;
    StopButton->Enabled     = TRUE;
    Display("Server is waiting for connections");
}
//---------------------------------------------------------------------------
// This event handler is triggered when server has been stopped, that is
// when server socket stop listening.
void __fastcall TAppBaseForm::HttpServer1ServerStopped(TObject *Sender)
{
    DocDirEdit->Enabled     = TRUE;
    DefaultDocEdit->Enabled = TRUE;
    PortEdit->Enabled       = TRUE;
    StartButton->Enabled    = TRUE;
    StopButton->Enabled     = FALSE;
    Display("Server stopped");
}
//---------------------------------------------------------------------------
// This event handler is triggered when a new client has connected.
void __fastcall TAppBaseForm::HttpServer1ClientConnect(
    TObject *Sender,                // HTTP server component
    TObject *Client,                // Client connecting
    WORD Error)                     //Error in connection
{
    ClientCountLabel->Caption = IntToStr(HttpServer1->ClientCount);
}
//---------------------------------------------------------------------------
// This event handler is triggered when a client is disconnecting, just
// before client component is closed.
void __fastcall TAppBaseForm::HttpServer1ClientDisconnect(
    TObject *Sender,                // HTTP server component
    TObject *Client,                // Client connecting
    WORD Error)                     //Error in connection
{
    ClientCountLabel->Caption = IntToStr(HttpServer1->ClientCount - 1);
}
//---------------------------------------------------------------------------
// This event handler is triggered when HTTP server component receive a HEAD
// command from any client.
// We just count the request, display a message and let HTTP server
// component handle everything.
// We should trap every URI we handle internally...
void __fastcall TAppBaseForm::HttpServer1HeadDocument(
    TObject *Sender,                // HTTP server component
    TObject *Client,                // Client connection issuing command
    THttpGetFlag &Flags)            // Tells what HTTP server has to do next
{
    FCountRequests = FCountRequests + 1;
    Display(IntToStr(FCountRequests) +
            ": HEAD " + ((TMyHttpConnection *)Client)->Path);
}
//---------------------------------------------------------------------------
// This event handler is triggered when HTTP server component receive a GET
// command from any client.
// We count the request, display a message and trap '/time.htm' path for
// special handling.
// There is no document time.htm on disk, we will create it on the fly. With
// a classic webserver we would have used a CGI or ISAPI/NSAPI to achieve
// the same goal. It is much easier here since we can use Delphi code
// directly to generate whatever we wants. Here for the demo we generate a
// page with server data and time displayed.
void __fastcall TAppBaseForm::HttpServer1GetDocument(
    TObject *Sender,                // HTTP server component
    TObject *Client,                // Client connection issuing command
    THttpGetFlag &Flags)            // Tells what HTTP server has to do nex
{
    // Count request and display a message
    FCountRequests++;
    Display(IntToStr(FCountRequests) +
            ": GET " + ((TMyHttpConnection *)Client)->Path);
    DisplayHeader((TMyHttpConnection *)Client);

    // Trap '/time.htm' path to dynamically generate an answer.
    if (CompareText(((THttpConnection *)Client)->Path, "/time.htm") == 0)
        CreateVirtualDocument_time_htm(Sender, Client, Flags);
}
//---------------------------------------------------------------------------
// This procedure is use to generate /time.htm document
void __fastcall TAppBaseForm::CreateVirtualDocument_time_htm(
    TObject *Sender,                // HTTP server component
    TObject *Client,                // Client connection issuing command
    THttpGetFlag &Flags)            // Tells what HTTP server has to do nex
{
    AnsiString Body;
    AnsiString Header;
    TMemoryStream *Stream;

    // Let HTTP server component know we will send data to client
    Flags  = hgWillSendMySelf;
    // Create a stream to hold data sent to client that is the answer
    // made of a HTTP header and a body made of HTML code.
    Stream = new TMemoryStream;
    Body   = "<HTML>"
                "<HEAD>"
                  "<TITLE>ICS WebServer Demo</TITLE>"
                "</HEAD>\r\n"
                "<BODY>"
                  "<H2>Time at server side:</H2>\r\n"
                  "<P>" + DateTimeToStr(Now()) +"</P>\r\n"
                "</BODY>"
              "</HTML>\r\n";
    Header = ((TMyHttpConnection *)Client)->Version + " 200 OK\r\n"
              "Content-Type: text/html\r\n"
              "Content-Length: \r\n" +
              IntToStr(Body.Length()) + "\r\n\r\n";
    Stream->Write(Header.data(), Header.Length());
    Stream->Write(Body.data(),   Body.Length());
    // We need to seek to start of stream !
    Stream->Seek(0, 0);
    // We ask server component to send the stream for us.
    ((TMyHttpConnection *)Client)->DocStream = Stream;
    ((TMyHttpConnection *)Client)->SendStream();
}
//---------------------------------------------------------------------------
void __fastcall TAppBaseForm::DisplayHeader(TMyHttpConnection *Client)
{
    int I;

    if (!DisplayHeaderCheckBox->Checked)
        return;
    for(I = 0; I < Client->RequestHeader->Count; I++)
        Display("HDR" + IntToStr(I + 1) + ") " +
                Client->RequestHeader->Strings[I]);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -