commit 1efca1a096e807622d173738acd37749bed74dfb
parent 73ce6e4704e63f76b6980069e2d12f22cf0901ce
Author: Dominik Schmidt <das1993@hotmail.com>
Date: Tue, 4 Aug 2015 22:28:37 +0200
Add a constructor with HTTP as argument.
The url now gets saved inside the HTTP-Structure of std.net.curl.
This allows for custom things to be done to the requests, like userAgents.
Diffstat:
drss/rss.d | | | 47 | +++++++++++++++++++++++++++++------------------ |
1 file changed, 29 insertions(+), 18 deletions(-)
diff --git a/drss/rss.d b/drss/rss.d
@@ -42,14 +42,22 @@ abstract class DRSS(T=Entry){
YES
};
DateReliable date_reliability=DateReliable.SEMI;
- immutable string url;
uint max_entries=100;
uint entry_count=0;
+ HTTP http;
+ ubyte buffer[];
abstract void parse(string document);
this(string url){
- this.url=url;
+ auto h=HTTP();
+ h.url=url;
+ this(h);
+ }
+ this(HTTP http){
+ this.http=http;
+ this.http.onReceive = &addContent;
+ this.http.onReceiveHeader = &parseHeader;
}
private bool checkIfNewEntry(in ref T newent){
@@ -87,7 +95,10 @@ abstract class DRSS(T=Entry){
final bool update(ubyte document[]=null){
if(document is null){
- document=fetch();
+ if(!fetch()){
+ return false;
+ }
+ document=buffer;
}
new_entries=0;
parse(cast(string)document);
@@ -122,28 +133,28 @@ abstract class DRSS(T=Entry){
public void discardNews(){
new_entries=0;
}
- public ubyte[] fetch(){
+ private void parseHeader(in char[] key, in char[] value){
+ if(key=="date"){
+ lastUpdated=value.dup;
+ }
+ }
+ private auto addContent(ubyte[]data){
+ buffer~=data;
+ return data.length;
+ }
+ public bool fetch(){
debug(RSS) writeln("Updateing");
- auto h=HTTP();
if(lastUpdated.length!=0){
debug(RSS) writeln("Adding if-not-modified-since");
- h.addRequestHeader("If-Modified-Since",lastUpdated);
+ http.addRequestHeader("If-Modified-Since",lastUpdated);
}
- h.url=url;
- ubyte document[];
- h.onReceive = (ubyte[] data) {document~=data; return data.length; };
- h.onReceiveHeader =(in char[] key, in char[] value){
- if(key=="date"){
- lastUpdated=value.dup;
- }
- };
debug(RSS) writeln("Performing HTTP request");
- h.perform();
- if(h.statusLine.code==200){
+ http.perform();
+ if(http.statusLine.code==200){
debug(RSS) writeln("Have news, parsing and returning xml");
- return document;
+ return true;
}
debug(RSS) writeln("Have no news, returning null");
- return null;
+ return false;
}
}