<div dir="ltr"><div>Python BaseHTTPServer by default uses HTTP/1.0 and it does not add content-length header automatically after switching to HTTP/1.1. Thanks for clarifying that.<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 9, 2016 at 5:23 PM, Aki Tuomi <span dir="ltr"><<a href="mailto:cmouse@youzen.ext.b2.fi" target="_blank">cmouse@youzen.ext.b2.fi</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">It works if you use HTTP/1.1 compatible responses. RFC says that you either<br>
need to send your response chunked or set content-length. YaHTTP will check<br>
and enforce this.<br>
<br>
Aki<br>
<div><div class="h5"><br>
On Sat, Jan 09, 2016 at 11:10:19AM +0200, Aleksey Chudov wrote:<br>
> Finally I found the cause of the problem. PowerDNS Remote HTTP Backend<br>
> works only with 'Content-Length' header.<br>
><br>
> So, the following code works until 'Content-Length' header exists.<br>
><br>
><br>
> import BaseHTTPServer<br>
><br>
> class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):<br>
>     def do_GET(self):<br>
>         if self.path == '/dns/lookup/<a href="http://example.com/SOA" rel="noreferrer" target="_blank">example.com/SOA</a>':<br>
>              self.wfile.write('HTTP/1.1 200 OK\r\nContent-Length:<br>
> 145\r\n\r\n{"result":[{"qtype":"SOA","qname":"<a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>","content":"<br>
> <a href="http://dns1.icann.org" rel="noreferrer" target="_blank">dns1.icann.org</a>. <a href="http://hostmaster.icann.org" rel="noreferrer" target="_blank">hostmaster.icann.org</a>. <a href="tel:2012080849" value="+12012080849">2012080849</a> 7200 3600 1209600<br>
> 3600","ttl":3600}]}')<br>
><br>
> if __name__ == '__main__':<br>
>     server = BaseHTTPServer.HTTPServer(('127.0.0.1', 8080),<br>
> HTTPRequestHandler)<br>
>     server.serve_forever()<br>
><br>
><br>
> I don't know whether this is bug in YaHTTP client used by PowerDNS or expected<br>
> behavior. But please add a note to the documentation.<br>
><br>
><br>
> On Fri, Jan 8, 2016 at 11:25 PM, Aleksey Chudov <<a href="mailto:aleksey.chudov@gmail.com">aleksey.chudov@gmail.com</a>><br>
> wrote:<br>
><br>
> > Some more information.<br>
> ><br>
> > I have tested pdns-static_3.4.7-1_amd64.deb on Debian and<br>
> > pdns-3.4.7-1.el7.x86_64 + pdns-backend-remote-3.4.7-1.el7.x86_64 on CentOS<br>
> > 7.2 with no success.<br>
> ><br>
> > Also I have tried manually reply to pdns request using netcat utility. As<br>
> > can be seen pdns sends next request just after headers + newline without<br>
> > waiting for the body.<br>
> ><br>
> ><br>
> > # grep -Ev '^$|^#' /etc/powerdns/pdns.conf<br>
> > launch=remote<br>
> > remote-connection-string=http:url=<a href="http://127.0.0.1:8080/dns,timeout=60000" rel="noreferrer" target="_blank">http://127.0.0.1:8080/dns,timeout=60000</a><br>
> > cache-ttl=0<br>
> > negquery-cache-ttl=0<br>
> > query-cache-ttl=0<br>
> ><br>
> ><br>
> > # dig @<a href="http://127.0.0.1" rel="noreferrer" target="_blank">127.0.0.1</a> <a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>. SOA<br>
> ><br>
> > ; <<>> DiG 9.7.3 <<>> @<a href="http://127.0.0.1" rel="noreferrer" target="_blank">127.0.0.1</a> <a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>. SOA<br>
> > ; (1 server found)<br>
> > ;; global options: +cmd<br>
> > ;; Got answer:<br>
> > ;; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 59226<br>
> > ;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 0<br>
> > ;; WARNING: recursion requested but not available<br>
> ><br>
> > ;; QUESTION SECTION:<br>
> > ;<a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>.                   IN      SOA<br>
> ><br>
> > ;; Query time: 0 msec<br>
> > ;; SERVER: 127.0.0.1#53(127.0.0.1)<br>
> > ;; WHEN: Fri Jan  8 23:22:47 2016<br>
> > ;; MSG SIZE  rcvd: 29<br>
> ><br>
> ><br>
> > # netcat -l 127.0.0.1 -p 8080<br>
> > GET /dns/lookup/<a href="http://example.com/SOA" rel="noreferrer" target="_blank">example.com/SOA</a> HTTP/1.1<br>
> > Accept: application/json<br>
> > Connection: Keep-Alive<br>
> > Host: 127.0.0.1<br>
> > User-Agent: YaHTTP v1.0<br>
> > X-Remotebackend-Local: 0.0.0.0<br>
> > X-Remotebackend-Real-Remote: <a href="http://127.0.0.1/32" rel="noreferrer" target="_blank">127.0.0.1/32</a><br>
> > X-Remotebackend-Remote: 127.0.0.1<br>
> > X-Remotebackend-Zone-Id: -1<br>
> ><br>
> > HTTP/1.1 200 OK<br>
> > Content-Type: text/javascript; charset=utf-8<br>
> ><br>
> > GET /dns/lookup/com/SOA HTTP/1.1<br>
> > Accept: application/json<br>
> > Connection: Keep-Alive<br>
> > Host: 127.0.0.1<br>
> > User-Agent: YaHTTP v1.0<br>
> > X-Remotebackend-Local: 0.0.0.0<br>
> > X-Remotebackend-Real-Remote: <a href="http://127.0.0.1/32" rel="noreferrer" target="_blank">127.0.0.1/32</a><br>
> > X-Remotebackend-Remote: 127.0.0.1<br>
> > X-Remotebackend-Zone-Id: -1<br>
> ><br>
> ><br>
> > Is this a bug or am I missing something?<br>
> ><br>
> ><br>
> > On Fri, Jan 8, 2016 at 11:42 AM, Aleksey Chudov <<a href="mailto:aleksey.chudov@gmail.com">aleksey.chudov@gmail.com</a>><br>
> > wrote:<br>
> ><br>
> >> Hi,<br>
> >><br>
> >> I'm trying to implement simple PowerDNS Remote HTTP Backend in Python.<br>
> >> Below is test code just like<br>
> >> <a href="https://doc.powerdns.com/md/authoritative/backend-remote/#scenario-soa-lookup-with-http-connector" rel="noreferrer" target="_blank">https://doc.powerdns.com/md/authoritative/backend-remote/#scenario-soa-lookup-with-http-connector</a><br>
> >><br>
> >><br>
> >> import BaseHTTPServer<br>
> >><br>
> >> REPLY = """{<br>
> >>   "result":<br>
> >>    [<br>
> >>      { "qtype": "SOA",<br>
> >>        "qname": "<a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>",<br>
> >>        "content": "<a href="http://dns1.icann.org" rel="noreferrer" target="_blank">dns1.icann.org</a>. <a href="http://hostmaster.icann.org" rel="noreferrer" target="_blank">hostmaster.icann.org</a>. <a href="tel:2012080849" value="+12012080849">2012080849</a> 7200<br>
> >> 3600 1209600 3600",<br>
> >>        "ttl": 3600,<br>
> >>        "domain_id": -1<br>
> >>      }<br>
> >>    ]<br>
> >> }"""<br>
> >><br>
> >> class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):<br>
> >>     def do_GET(self):<br>
> >>         if self.path == '/dns/lookup/<a href="http://example.com/SOA" rel="noreferrer" target="_blank">example.com/SOA</a>':<br>
> >>             self.send_response(200)<br>
> >>             self.send_header('Content-Type', 'text/javascript;<br>
> >> charset=utf-8')<br>
> >>             self.end_headers()<br>
> >>             self.wfile.write(REPLY + '\r\n')<br>
> >>         else:<br>
> >>             self.send_error(501)<br>
> >><br>
> >> if __name__ == '__main__':<br>
> >>     server = BaseHTTPServer.HTTPServer(('127.0.0.1', 8080),<br>
> >> HTTPRequestHandler)<br>
> >>     server.serve_forever()<br>
> >><br>
> >><br>
> >> Looks pretty simple. And curl shows the result.<br>
> >><br>
> >><br>
> >> # curl -i <a href="http://127.0.0.1:8080/dns/lookup/example.com/SOA" rel="noreferrer" target="_blank">http://127.0.0.1:8080/dns/lookup/example.com/SOA</a><br>
> >> HTTP/1.0 200 OK<br>
> >> Server: BaseHTTP/0.3 Python/2.7.5<br>
> >> Date: Fri, 08 Jan 2016 09:24:54 GMT<br>
> >> Content-Type: text/javascript; charset=utf-8<br>
> >><br>
> >> {<br>
> >>   "result":<br>
> >>    [<br>
> >>      { "qtype": "SOA",<br>
> >>        "qname": "<a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>",<br>
> >>        "content": "<a href="http://dns1.icann.org" rel="noreferrer" target="_blank">dns1.icann.org</a>. <a href="http://hostmaster.icann.org" rel="noreferrer" target="_blank">hostmaster.icann.org</a>. <a href="tel:2012080849" value="+12012080849">2012080849</a> 7200<br>
> >> 3600 1209600 3600",<br>
> >>        "ttl": 3600,<br>
> >>        "domain_id": -1<br>
> >>      }<br>
> >>    ]<br>
> >> }<br>
> >><br>
> >> And Python script prints<br>
> >><br>
> >> 127.0.0.1 - - [08/Jan/2016 11:36:31] "GET /dns/lookup/<a href="http://example.com/SOA" rel="noreferrer" target="_blank">example.com/SOA</a><br>
> >> HTTP/1.1" 200 -<br>
> >><br>
> >><br>
> >> So, I've installed PoweDNS from CentOS 7 Epel repository<br>
> >><br>
> >> # rpm -qa pdns\*<br>
> >> pdns-3.4.7-1.el7.x86_64<br>
> >> pdns-backend-remote-3.4.7-1.el7.x86_64<br>
> >><br>
> >><br>
> >> And my configuration file contains only the following settings<br>
> >><br>
> >> # grep -Ev '^#|^$' /etc/pdns/pdns.conf<br>
> >> setuid=pdns<br>
> >> setgid=pdns<br>
> >> launch=remote<br>
> >> remote-connection-string=http:url=<a href="http://127.0.0.1:8080/dns" rel="noreferrer" target="_blank">http://127.0.0.1:8080/dns</a><br>
> >> cache-ttl=0<br>
> >> negquery-cache-ttl=0<br>
> >> query-cache-ttl=0<br>
> >><br>
> >><br>
> >> But my remote backend does not work!<br>
> >><br>
> >><br>
> >> # dig @<a href="http://127.0.0.1" rel="noreferrer" target="_blank">127.0.0.1</a> <a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>. SOA<br>
> >><br>
> >> ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.1 <<>> @<a href="http://127.0.0.1" rel="noreferrer" target="_blank">127.0.0.1</a> <a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>. SOA<br>
> >> ; (1 server found)<br>
> >> ;; global options: +cmd<br>
> >> ;; Got answer:<br>
> >> ;; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 5375<br>
> >> ;; flags: qr rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1<br>
> >> ;; WARNING: recursion requested but not available<br>
> >><br>
> >> ;; OPT PSEUDOSECTION:<br>
> >> ; EDNS: version: 0, flags:; udp: 1680<br>
> >> ;; QUESTION SECTION:<br>
> >> ;<a href="http://example.com" rel="noreferrer" target="_blank">example.com</a>.                   IN      SOA<br>
> >><br>
> >> ;; Query time: 2 msec<br>
> >> ;; SERVER: 127.0.0.1#53(127.0.0.1)<br>
> >> ;; WHEN: Fri Jan 08 11:38:05 EET 2016<br>
> >> ;; MSG SIZE  rcvd: 40<br>
> >><br>
> >><br>
> >> There is no new messages from pdns_server<br>
> >><br>
> >> # systemctl status pdns.service<br>
> >> ● pdns.service - PowerDNS Authoritative Server<br>
> >>    Loaded: loaded (/usr/lib/systemd/system/pdns.service; enabled; vendor<br>
> >> preset: disabled)<br>
> >>    Active: active (running) since Fri 2016-01-08 11:37:50 EET; 18s ago<br>
> >>   Process: 1581 ExecStop=/usr/bin/pdns_control quit (code=exited,<br>
> >> status=0/SUCCESS)<br>
> >>   Process: 1586 ExecStart=/usr/sbin/pdns_server --daemon (code=exited,<br>
> >> status=0/SUCCESS)<br>
> >>  Main PID: 1587 (pdns_server)<br>
> >>    CGroup: /system.slice/pdns.service<br>
> >>            └─1587 /usr/sbin/pdns_server --daemon<br>
> >><br>
> >> Jan 08 11:37:50 localhost pdns[1587]: Listening on controlsocket in<br>
> >> '/var/run/pdns.controlsocket'<br>
> >> Jan 08 11:37:50 localhost systemd[1]: Started PowerDNS Authoritative<br>
> >> Server.<br>
> >> Jan 08 11:37:50 localhost pdns[1587]: UDP server bound to <a href="http://0.0.0.0:53" rel="noreferrer" target="_blank">0.0.0.0:53</a><br>
> >> Jan 08 11:37:50 localhost pdns[1587]: TCP server bound to <a href="http://0.0.0.0:53" rel="noreferrer" target="_blank">0.0.0.0:53</a><br>
> >> Jan 08 11:37:51 localhost pdns[1587]: PowerDNS Authoritative Server 3.4.7<br>
> >> (<a href="mailto:jenkins@autotest.powerdns.com">jenkins@autotest.powerdns.com</a>) (C) 2001-2015 PowerDNS.COM BV<br>
> >> Jan 08 11:37:51 localhost pdns[1587]: Using 64-bits mode. Built on<br>
> >> 20151108152440 by <a href="mailto:mockbuild@buildvm-19.phx2.fedoraproject.org">mockbuild@buildvm-19.phx2.fedoraproject.org</a>, gcc 4.8.3<br>
> >> 20140911 (Red Hat 4.8.3-9).<br>
> >> Jan 08 11:37:51 localhost pdns[1587]: PowerDNS comes with ABSOLUTELY NO<br>
> >> WARRANTY. This is free software, and you are welcome to redistribute it<br>
> >> according to the terms of the GPL version 2.<br>
> >> Jan 08 11:37:51 localhost pdns[1587]: Creating backend connection for TCP<br>
> >> Jan 08 11:37:51 localhost pdns[1587]: About to create 3 backend threads<br>
> >> for UDP<br>
> >> Jan 08 11:37:51 localhost pdns[1587]: Done launching threads, ready to<br>
> >> distribute questions<br>
> >><br>
> >><br>
> >> But Python prints<br>
> >><br>
> >> 127.0.0.1 - - [08/Jan/2016 11:38:05] "GET /dns/lookup/<a href="http://example.com/SOA" rel="noreferrer" target="_blank">example.com/SOA</a><br>
> >> HTTP/1.1" 200 -<br>
> >> 127.0.0.1 - - [08/Jan/2016 11:38:05] code 501, message Not Implemented<br>
> >> 127.0.0.1 - - [08/Jan/2016 11:38:05] "GET /dns/lookup/com/SOA HTTP/1.1"<br>
> >> 501 -<br>
> >> 127.0.0.1 - - [08/Jan/2016 11:38:05] code 501, message Not Implemented<br>
> >> 127.0.0.1 - - [08/Jan/2016 11:38:05] "GET /dns/lookup//SOA HTTP/1.1" 501 -<br>
> >><br>
> >><br>
> >> Where am I wrong?<br>
> >><br>
> >><br>
> >> Regards,<br>
> >> Aleksey<br>
> >><br>
> ><br>
> ><br>
<br>
</div></div>> _______________________________________________<br>
> Pdns-users mailing list<br>
> <a href="mailto:Pdns-users@mailman.powerdns.com">Pdns-users@mailman.powerdns.com</a><br>
> <a href="http://mailman.powerdns.com/mailman/listinfo/pdns-users" rel="noreferrer" target="_blank">http://mailman.powerdns.com/mailman/listinfo/pdns-users</a><br>
<br>
</blockquote></div><br></div></div>