โŒ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayNews from the Ada programming language world

How to add Headers in AWS (ada web server)?

I have been trying to add Headers (specifically for CORS) to my server response for hours now, but I cant find a way in ada with AWS. Can someone help me? ( So far I have found only ways to add the Headers to the Client, but not as a Server Response)

Please ignore that the code is a bit messy, this is my first real project in Ada and I am just trying things out.

with AWS.Client;
with AWS.Response;
with AWS.Server;
with AWS.Server.Status;
with AWS.Status;
with Ada.Text_IO;
with Ada.Strings.Unbounded;
with AWS.Headers;

procedure aws_test is:

   WS      : AWS.Server.HTTP;
   Headers : AWS.Headers.List;

   function HW_CB (Request : in AWS.Status.Data) return AWS.Response.Data is
      URI : constant String := AWS.Status.URI (Request);

      Test_Data : AWS.Response.Data :=
        AWS.Client.Get (URL => "http://192.168.190.129");

      Test_String   : constant String := AWS.Response.Message_Body (Test_Data);
      para          : Ada.Strings.Unbounded.Unbounded_String;
      test          : Integer         := 1;
      response_data : AWS.Response.Data;

    begin

      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Origin",
         Value => "*");
      -- Allow common methods
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Methods",
         Value => "POST, GET, OPTIONS, PUT, DELETE");

      -- Allow all headers requested in the actual request
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Allow-Headers",
         Value => "*");

      -- Set max age to 86400 seconds (24 hours)
      AWS.Headers.Add
        (Table => Headers, Name => "Access-Control-Max-Age", Value => "86400");

      if URI = "/api/login" then
         -- ADD HERE
         test := test + 1;
         para := AWS.Status.Binary_Data (Request);

         Ada.Text_IO.Put_Line (Ada.Strings.Unbounded.To_String (para));
         -- Ada.Text_IO.Put_Line (Test_String);

         response_data := AWS.Response.Build ("text/html", "<p>Hello world !");
         AWS.Response.Set.Add_Header
           (response_data, Name => "Access-Control-Max-Age", Value => "86400");
         return response_data;
      else
         return AWS.Response.Build ("text/html", "<p>Hum...");
      end if;
    end HW_CB;

    begin
     AWS.Server.Start
      (WS, "Hotel AI", Callback => HW_CB'Unrestricted_Access, Port => 8_080);
     delay 20.0;
     AWS.Server.Shutdown (WS);
     while AWS.Server.Status.Is_Shutdown (WS) = False loop
      delay 5.0;
     end loop;
    end aws_test;

I just would like to add Headers so I dont get CORS flagged in Javascript. Thank you very much for your Help ( I am a bit struggling to find good resources on these topics in the web )

I have already tried several possible ways (as well with the help of AI etc.), bu tI couldn't even really find good examples on Github or in the documentation

โŒ
โŒ