Swi prolog webserver

in the file: http_header.pl i changed this function below to be able to insert tags inside the html files which will output special html , database fields etc.
is this a bad idea ? is it dangerous or can i do this in this way

reply_file(Out, File, Header, _) :-
    setup_call_cleanup(
        open(File, read, In, [type(binary)]),

    reply_file_line_by_line(0, In, Out, Header ),

        close(In)).

process_line( Lx , Out ):-
  sub_string( Lx, _P,_,_, "[jajaja]" ) , !, 
    write( Out, "prolog change prolog change " ), write( Out, "\n" ) .

process_line( Lx , Out ):- !,
 write( Out, Lx ), write( Out, "\n" ) .
%----
reply_file_line_by_line( 0 , Sea , Out , Header ):- !,
 send_reply_header(Out, Header),
 reply_file_line_by_line( 1 , Sea , Out , Header ).

reply_file_line_by_line( Cou , Sea , Out , Header ):- not(at_end_of_stream(Sea)),
 read_line_to_string( Sea, Lx ) , Lx \= end_of_file , ! ,
   process_line( Lx , Out ),
   % write( Out, Lx ), write( Out, "\n" ) ,
   Cou2 is Cou + 1 , 
   reply_file_line_by_line( Cou2 , Sea, Out, Header ).
reply_file_line_by_line( Cou , Sea, Out, Header):- not(at_end_of_stream(Sea)),!, reply_file_line_by_line( Cou, Sea, Out, Header ).
reply_file_line_by_line( _, _ , _ , _ ):- !.

I don’t have a complete overview, but it sounds unnecessary and dangerous. I suspect the reply file gets the proper length reply from the size of the file. You are disturbing that. If you want to reply using replacements, simply write a handler that reads the file, make the necessary replacements while writing to current_output. I.e., something like this

reply_html_with_replacements(_Request, ...) :-
    format('Content-type: text/html~n~n'),
    setup_call_cleanup(
        open(File, In, read),
        copy_and_replace(In, ...),
        close(In)).

thankyou for your answer, it is of great help. i was also afraid i would break the server https -encryption in this way