rtcphp.pas

来自「Delphi快速开发Web Server」· PAS 代码 · 共 677 行 · 第 1/2 页

PAS
677 行
字号
      php_register_variable('QUERY_STRING', PChar(php.Request.Query.Text), val, p);

      if php.Request.Query.Text='' then
        php_register_variable('REQUEST_URI', PChar(php.Request.FileName), val, p)
      else
        php_register_variable('REQUEST_URI', PChar(php.Request.URI), val, p);

      php_register_variable('SCRIPT_NAME', PChar(php.Request.FileName), val, p);
      php_register_variable('PHP_SELF', PChar(php.Request.FileName), val, p);

      php_register_variable('SERVER_NAME', PChar(php.Request.Host), val, p);

      for i:=0 to php.Request.ItemCount-1 do
        begin
        vname:=StringReplace(php.Request.ItemName[i],'-','_',[rfReplaceAll]);
        vval:=php.Request.ItemValue[i];
        php_register_variable(PChar('HTTP_'+vname), PChar(vval), val, p);
        end;
      end;

    if php.LocalAddr<>'' then    php_register_variable('SERVER_ADDR', PChar(php.LocalAddr), val, p);
    if php.LocalPort<>'' then    php_register_variable('SERVER_PORT', PChar(php.LocalPort), val, p);
    if php.PeerAddr<>'' then     php_register_variable('REMOTE_ADDR', PChar(php.PeerAddr), val, p);
    if php.PeerPort<>'' then     php_register_variable('REMOTE_PORT', PChar(php.PeerPort), val, p);
    if php.DocumentRoot<>'' then php_register_variable('DOCUMENT_ROOT', PChar(php.DocumentRoot), val, p);
    if php.FileName<>'' then     php_register_variable('SCRIPT_FILENAME', PChar(php.FileName), val, p);
  except
    on E:Exception do
      begin
      Log('Register Variables callback',E,'PHP');
      end;
    end;
  end;

procedure php_delphi_send_header(p1:Psapi_header_struct; p2:pointer); cdecl;
  var
    php : TRtcPHP;
    gl : psapi_globals_struct;
    ts : pointer;
    s : string;
  begin
  try
    if not assigned(p1) then Exit;

    ts := GetResource;
    if not assigned(ts) then Exit;
    gl := GetSAPIGlobals(ts);
    if not assigned(gl) then Exit;
    php := TRtcPHP(gl^.server_context);
    if not assigned(php) then Exit;

    s:=p1^.header;
    php.ResultHeader:=php.ResultHeader+s+#13#10;
  except
    on E:Exception do
      Log('Send Header callback',E,'PHP');
    end;
  end;

function php_delphi_read_cookies(p1 : pointer) : pointer; cdecl;
  var
    php : TRtcPHP;
    gl : psapi_globals_struct;
    ts : pointer;
  begin
  Result:=nil;
  try
    ts := GetResource;
    if not assigned(ts) then Exit;
    gl := GetSAPIGlobals(ts);
    if not assigned(gl) then Exit;
    php := TRtcPHP(gl^.server_context);
    if not assigned(php) then Exit;

    if php.Request.Value['COOKIE']<>'' then
      Result:=PChar(php.Request.Value['COOKIE']);
  except
    on E:Exception do
      begin
      Result:=nil;
      Log('Read Cookies callback',E,'PHP');
      end;
    end;
  end;

function php_delphi_read_post(p1:pointer; count_bytes:integer) : integer; cdecl;
  var
    php : TRtcPHP;
    gl : psapi_globals_struct;
    ts : pointer;
    pc : PChar;
  begin
  Result:=0;
  try
    ts := GetResource;
    if not assigned(ts) then Exit;
    gl := GetSAPIGlobals(ts);
    if not assigned(gl) then Exit;
    php := TRtcPHP(gl^.server_context);
    if not assigned(php) then Exit;

    if (length(php.RequestBody)>php.RequestBodyOut) then
      begin
      if count_bytes <= length(php.RequestBody)-php.RequestBodyOut then
        begin
        if count_bytes>0 then
          Move(php.RequestBody[php.RequestBodyOut+1], p1^, count_bytes);
        Result:=count_bytes;
        php.RequestBodyOut:=php.RequestBodyOut+count_bytes;
        end
      else
        begin
        Result:=length(php.RequestBody)-php.RequestBodyOut;
        pc:=PChar(Copy(php.RequestBody, php.RequestBodyOut+1, Result));
        Move(pc^,p1^,Result+1);
        php.RequestBodyOut:=php.RequestBodyOut+Result;
        end;
      end;
  except
    on E:Exception do
      begin
      Result:=0;
      Log('Read Post callback',E,'PHP');
      end;
    end;
  end;

{ TRtcPHP }

constructor TRtcPHP.Create;
  begin
  inherited;
  FSessionActive := false;
  end;

destructor TRtcPHP.Destroy;
  begin
  ShutdownRequest;
  inherited;
  end;

function TRtcPHP.Execute : string;
  var
    file_handle : zend_file_handle;
    a,len:integer;

  begin
  FBuffer := TStringList.Create;
  FBufferSize := 0;

  try
    if not FileExists(FFileName) then
      raise EPHPError.CreateFmt('File %s does not exists', [FFileName]);

    FillChar(file_handle, sizeof(zend_file_handle), 0);
    file_handle._type := ZEND_HANDLE_FILENAME;
    file_handle.filename := PChar(FFileName);
    file_handle.opened_path := nil;
    file_handle.free_filename := 0;

    ResultHeader:='';

    StartupRequest;
    try
      php_execute_script(@file_handle, TSRMLS_D);
    finally
      ShutdownRequest;
      end;
  except
    on E:Exception do
      begin
      FBuffer.Add('<p>ERROR!'+E.Message+'</p>');
      Inc(FBufferSize, length(FBuffer.Strings[FBuffer.Count-1]));
      end;
    end;

  Setlength(Result,FBufferSize);
  len:=1;
  for a:=0 to FBuffer.Count-1 do
    begin
    Move(FBuffer.Strings[a][1],Result[len],length(FBuffer.Strings[a]));
    Inc(len,length(FBuffer.Strings[a]));
    end;

  FBuffer.Free;
  end;

procedure TRtcPHP.StartupRequest;
  var
    gl  : psapi_globals_struct;
  begin
  if FSessionActive then Exit;

  InitResource;

  try
    TSRMLS_D := GetResource;

    PG(TSRMLS_D)^.register_globals := true;

    gl := GetSAPIGlobals(TSRMLS_D);

    with gl^ do
      begin
      RequestBodyOut:=0;

      server_context := Self;
      if Request.Method='HEAD' then
        request_info.request_method := 'GET'
      else
        request_info.request_method := PChar(Request.Method);

      request_info.query_string := PChar(Request.Query.Text);
      request_info.content_type := PChar(Request['CONTENT-TYPE']);
      request_info.content_length := length(RequestBody);

      read_post_bytes:=length(RequestBody);

      request_info.request_uri := PChar(Request.FileName);
      sapi_headers.http_response_code := 200;
      end;

    php_request_startup(TSRMLS_D);

    FSessionActive := true;
  except
    FSessionActive := false;

    FreeResource;
    end;
  end;

procedure TRtcPHP.ShutdownRequest;
  begin
  if not FSessionActive then Exit;

  try
    php_request_shutdown(TSRMLS_D);
  finally
    FreeResource;
    FSessionActive := False;
    end;
  end;

procedure StartupModule;
  begin
  delphi_sapi_module.name := 'embed';  //to solve a problem with dl()
  delphi_sapi_module.pretty_name := 'RTC PHP Server';  (* pretty name *)
  delphi_sapi_module.startup := php_delphi_startup;    (* startup *)
  delphi_sapi_module.shutdown := php_module_shutdown_wrapper;   (* shutdown *)
  delphi_sapi_module.activate:= nil;  (* activate *)
  delphi_sapi_module.deactivate := @php_delphi_deactivate;  (* deactivate *)
  delphi_sapi_module.ub_write := @php_delphi_ub_write;      (* unbuffered write *)
  delphi_sapi_module.flush := nil;
  delphi_sapi_module.stat:= nil;
  delphi_sapi_module.getenv:= nil;
  delphi_sapi_module.sapi_error := @zend_error;  (* error handler *)
  delphi_sapi_module.header_handler := nil;
  delphi_sapi_module.send_headers := nil;
  delphi_sapi_module.send_header :=  @php_delphi_send_header;
  delphi_sapi_module.read_post := @php_delphi_read_post;
  delphi_sapi_module.read_cookies := @php_delphi_read_cookies;
  delphi_sapi_module.register_server_variables := @php_delphi_register_variables;   (* register server variables *)
  delphi_sapi_module.log_message := nil;  (* Log message *)
  if ini_folder <> '' then
    delphi_sapi_module.php_ini_path_override := PChar(ini_folder)
  else
    delphi_sapi_module.php_ini_path_override :=  nil;
  delphi_sapi_module.block_interruptions := nil;
  delphi_sapi_module.unblock_interruptions := nil;
  delphi_sapi_module.default_post_reader := nil;
  delphi_sapi_module.treat_data := nil;
  delphi_sapi_module.executable_location := nil;
  delphi_sapi_module.php_ini_ignore := 0;

  sapi_startup(@delphi_sapi_module);
  php_module_startup(@delphi_sapi_module, nil, 0{1});
  end;

procedure ShutdownModule;
  begin
  delphi_sapi_module.shutdown(@delphi_sapi_module);
  sapi_shutdown;
  end;

procedure StartupPHP(DLLFolder, IniFolder:string);
  begin
  CS.Enter;
  try
    if not PHPLoaded then
      begin
      if DLLFolder <> '' then
        if Copy(DLLFolder,length(DLLFolder),1)<>'\' then
          DLLFolder:=DLLFolder+'\';

      LoadPHP(DLLFolder);

      ThrList:=tBinList.Create(128);

      tsrm_startup(512, 512, TSRM_ERROR_LEVEL_ERROR , nil);

      ini_folder := IniFolder;
      StartupModule;

      module_active:=True;
      end;
  finally
    CS.Leave;
    end;
  end;

procedure ShutDownPHP;
  begin
  CS.Enter;
  try
    if module_active then
      begin
      ShutDownModule;

      tsrm_shutdown();

      module_active:=false;
      UnloadPHP;

      Garbage(ThrList);
      end;
  finally
    CS.Leave;
    end;
  end;

initialization
CS:=TRtcCritSec.Create;
finalization
ShutDownPHP;
Garbage(CS);
end.

⌨️ 快捷键说明

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