如何在 Prolog Web 服务器上转义双引号

How to Escape Double Quotes on Prolog Web Server

提问人:Lucian Green 提问时间:2/23/2022 更新时间:2/23/2022 访问量:64

问:

我使用以下 Prolog 代码来显示 HTML 表单,但当表单项包含双引号时它不起作用。如何避免使用双引号?info_server(8000).

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/html_write)).

:- use_module(library(http/http_client)).
:- http_handler('/', info_web_form, []).

info_server(Port) :-
        http_server(http_dispatch, [port(Port)]).

/*
browse http://localhost:8000/   
*/

       info_web_form(_Request) :-
                                                                                                                      format('Content-type: text/html~n~n', []),
                                                                            
data(Header,Footer), 

加载网页的页眉和页脚,并在不同的页面上重复使用

format(Header,[]),

显示标题

middle_form,

显示窗体

format(Footer,[]).

显示页脚

                                                                                                  % When the form is submitted, it displays the landing page.
:- http_handler('/landing', landing_pad, []).

                                                                                                      landing_pad(Request) :-
                                                                                                              member(method(post), Request), !,
                                                                                                                  http_read_data(Request, Data, []),
                

                format('Content-type: text/html~n~n', []),

Data=[input=Input1, info=Hidden1, submit=_],
    
format(Input1,[]),

format(Hidden1,[]),

显示窗体中的隐藏数据,该数据因包含双引号而被截断。

data(Header,Footer),

format(Header,[]),


middle_form,    

format(Footer,[]).


data(Header,Footer) :-

Header='<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html lang="en">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>Title</title>
    <style type="text/css"> 
<!-- 

A:link {text-decoration: none;} 
A:visited {text-decoration: none;} 
A:hover {text-decoration: underline;} 

img {
 height: auto;
 max-width: 100%;
 object-fit: contain;
} 

table {table-layout: fixed; width: 100%;}

td {word-wrap: break-word;}

--> 
  </style>
  <!-- Displays text at readable size on both desktops and mobiles -->
    <meta name="viewport" content="width=device-width, initial-scale=1">

  </head>
  <!-- salmon coloured -->
  <body style="background-color: rgb(255, 239, 227);">

   
    <div style="text-align: center;">
      <table width="80%">
        <tbody>
          <tr>
            <td>
              <p>',

Footer='</p>
            </td>
          </tr>
        </tbody>
      </table>
      <br>

    <br>
  </body>
</html>'.


middle_form :-

下面是隐藏的数据,用双引号截断。

Hidden=[[a],"A",'a'],
    term_to_atom(Hidden,Hidden1),

Hidden1=Hidden2,
    concat_list(["
       
  <form action=\"/landing\" method=\"POST\">
  <label for=info></label>
  <input type=text id=input name=input value=''><br><br>
  <input type=hidden id=info name=info value=\"",Hidden2,"\"><br><br>
  <input type=submit name=submit value='Submit'>
</form>
"],Form_text1),
  

对下面atom_string的调用将表单显示为原子。

atom_string(Form_text,Form_text1),
    format(Form_text,[]).
    
    

例如,replace_new谓词将字符串替换为字符串中的字符串 replace_new('0ab0','a','c',A). A = “0cb0”。

replace_new(A1,Find,Replace,F) :-
string_length(Replace,Replace_l),
string_concat("%",A1,A2),
string_concat(A2,"%",A),        split_string(A,Find,Find,B),findall([C,Replace],(member(C,B)),D),maplist(append,[D],[E]),concat_list(E,F1),string_concat(F2,G,F1),string_length(G,Replace_l),
    string_concat("%",F3,F2),   
    string_concat(F,"%",F3),!.
    

concat_list将字符串列表连接到字符串,例如: concat_list([“a”,“b”,“c”],D). D=“abc”

concat_list([],""):- !.
concat_list(A1,B):-
    concat_list("",A1,B),!.

concat_list(A,List,B) :-
    concat_list1(A,List,B).

concat_list1(A,[],A):- !.
concat_list1(A,List,B) :-
    List=[Item|Items],
    string_concat(A,Item,C),
    concat_list1(C,Items,B).

当我查看网页时,隐藏的信息值在登录页面上显示时会部分丢失。http://localhost:8000/

服务器 Prolog 转义 引号

评论


答:

0赞 Lucian Green 2/23/2022 #1

我研究了如何转义双引号,它涉及将上面的代码从:

Hidden1=Hidden2,

自:

replace_new(Hidden1,"\"","&quot;",Hidden2), 

进行这些更改会将双引号替换为表单数据并保留表单数据。&quot;

注意:这也假设程序员在 中表单数据周围使用双引号,从而消除了导致错误的数据中的单引号。<input type=hidden id=info name=info value=\"",Hidden2,"\">