Using OnCustomResponseEvent in a ROSDK Server

example for plain HTTPServer.OnCustomResponseEvent.
This snippet shows how to handle unknown requests or requests with incomplete data.
TROPostMessage was used

procedure TServerMainForm.ROServerCustomResponseEvent(
  const aTransport: IROHTTPTransport; const aRequestStream,
  aResponseStream: TStream; const aResponse: IROHTTPResponse;
  var aHandled: Boolean);
var
  lMessage:  IROMessage;
  op: TROResponseOptions;
  s: AnsiString;
begin
  // handle all requests started with 'xxxx':
  if pos('/xxxx',aTransport.PathInfo) = 1 then begin  // "xxxx" shouldn't be defined in Server.Dispathers
    
   //1. Pre-processing data 
{ 
   // * extracting data if GET method was used
   s := aTransport.QueryString;
    
  // * extracting data if POST method was used
    SetLength(s, aRequestStream.Size);
    aResponseStream.Position := 0;
    aRequestStream.Read(Pointer(s)^, Length(s));

   // modify data in some way
    s := '__MessageType=Message&__InterfaceName=MyService&'+s;

   // write updated data back to stream
    aRequestStream.Size := 0;
    aRequestStream.Write(Pointer(s)^, Length(s));
}
    // 2. Invoke service method 
    lMessage:= (ROPOSTMessage as IROMessageCloneable).Clone;
    MainProcessMessage(lMessage,atransport,aRequestStream,aResponseStream,op);

    //3. Post-processing data
{    SetLength(s, aResponseStream.Size);
    aResponseStream.Read(Pointer(s)^, Length(s));

    // modify data again
    s := s + my_some_data;

    aResponseStream.Size := 0;
    aResponseStream.Write(Pointer(s)^, Length(s));
}
    aHandled := True;
  end;
end;
3 Likes