问题
I am not good with delphi yet, but based on some examples I have managed to create simple http server with no more than 10 users.
There are 2 main problems I don't know how to solve yet.
- proper way to authenticate, manage users - sessions
- main problem, connection must be secure, so SSL encryption is needed, how to implement it?
Any example I found in relation with idhttpserver and openssl, was not quite complete or with older version of Indy.
I am currently working with Delphi XE2 with Indy 10 components.
回答1:
proper way to authenticate, manage users - sessions
TIdHTTPServer manages HTTP sessions for you if you set the TIdHTTPServer.SessionState property is true (it is false by default). TIdHTTPServer uses cookies for session management, so your clients need to have cookies enabled.
Authentication has to be performed manually, but how you do that depends on whether your clients are using HTTP-based or HTML-based authentication.
For HTTP authentication, there are ARequestInfo.UserName and ARequestInfo.Password properties available. If not valid, send an appropriate 401 response back to the client (if you set the AResponseInfo.AuthRealm property to a non-blank string, TIdHTTPServer will send a 401 response for you). By default, TIdHTTPServer only supports BASIC authentication. If you want to support other authentication schemes, you will have to use the TIdHTTPServer.OnParseAuthentication event, and send the 401 reply manually so you can send back appropriate WWW-Authenticate headers. Either way, if the client is validated, you can use HTTP sessions to keep the client logged in between requests. The AResponseInfo.Session and AResponseInfo.Session properties point at the current session. If TIdHTTPServer.AutoStartSession is true (it is false by default), TIdHTTPServer creates new sessions automatically. Otherwise, you can call TIdHTTPServer.CreateSession() yourself when needed. TIdHTTPSession has a Content property that you can store session-specific data in. Or you can derive a new class from TIdHTTPSession and then use the TIdHTTPServer.OnCreateSession event to create instances of that class.
For HTML authentication, you have two choices, depending on how you configure your HTML:
if your HTML
<form>tag does not have anenctypeattribute, or it is set toapplication/x-www-webform-urlencoded,TIdHTTPServerwill store the raw webform data in theARequestInfo.FormParamsproperty, and ifTIdHTTPServer.ParseParamsis true (which it is by default), the data will also be parsed into theARequestInfo.Paramsproperty for you.if your HTML
<form>tag has anenctypeattribute set tomultipart/form-data, you will have to parse the content of theARequestInfo.PostStreammanually, asTIdHTTPServerdoes not yet parse that data for you (examples have been posted many times before on many different forums on how to parse that data manually using Indy'sTIdMessageDecoderMIMEclass). By default,ARequestInfo.PostStreampoints at aTMemoryStreamobject. You can use theTIdHTTPServer.OnCreatePostStreamevent to create an instance of a differentTStream-derived class, if desired.
main problem, connection must be secure, so SSL encryption is needed, how to implement it?
Before activating the server:
assign a
TIdServerIOHandlerSSLBase-derived component, such asTIdServerIOHandlerSSLOpenSSL, to theTIdHTTPServer.IOHandlerproperty and configure it as needed (certificate, peer validation, SSL version(s), etc). In the case of OpenSSL, you will have to deploy the 2 OpenSSL library binarieslibeay32.dllandssleay32.dll(or non-Windows platform equivalents) with your app if they are not already pre-installed on the target OS, or if you want to ensure your app uses a specific version of OpenSSL. At this time, OpenSSL is the only encryption that Indy supports natively, but there are third-party solutions available that are compatible with Indy, such as EldoS SecureBlackbox.fill in the
TIdHTTPServer.Bindingproperty with a binding for your desired HTTPS port (443 is the default HTTPS port). Typically you should create 2 bindings, one for HTTP port 80 and one for HTTPS port 443. Inside yourOnCommand...handlers, if you receive a request that requires SSL/TLS encryption, you can check the port that the request was made on (AContext.Binding.Port) and if not HTTPS then redirect (AResponseInfo.Redirect()) the client to retry the request on the HTTPS port.assign a handler to the
TIdHTTPServer.OnQuerySSLPortevent and have it set itsVUseSSLparameter to True when itsAPortparameter matches your HTTPS port. UPDATE starting with SVN rev 5461, anOnQuerySSLPorthandler is no longer needed if your only HTTPS port is 443.
来源:https://stackoverflow.com/questions/34415978/creating-idhttpserver-with-ssl-encryption