Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

目录级替换如果找不到匹配内容,尝试访问网络

  • Loading branch information...
commit 63392819f7b1b0d5538ed2bf5dc178be106c8bf4 1 parent 021e4d8
@iptton iptton authored
View
8 proxy/ryconnection.cpp
@@ -645,7 +645,13 @@ bool RyConnection::checkRule(RyPipeData_ptr& pipe){
_connectingHost = rule->replace();
_sendingPipeData->replacedHost = _connectingHost;
}else{
- QPair<QByteArray,QByteArray> headerAndBody = manager->getReplaceContent(rule,_sendingPipeData->fullUrl);
+ bool isResouceFound = true;
+ QPair<QByteArray,QByteArray> headerAndBody = manager->getReplaceContent(rule,_sendingPipeData->fullUrl,&isResouceFound);
+ // TODO check settings if go through when local replace match but got 404
+ if(rule->type() == RyRule::LOCAL_DIR_REPLACE && !isResouceFound){
+ return false;
+ }
+
//qDebug()<<headerAndBody.second;
//qDebug()<<headerAndBody.first;
bool isOk;
View
4 rule/ryrulemanager.cpp
@@ -264,9 +264,9 @@ QList<QSharedPointer<RyRule> > RyRuleManager::getMatchRules(const QString& url){
}
//返回header body
-QPair<QByteArray,QByteArray> RyRuleManager::getReplaceContent(QSharedPointer<RyRule> rule,const QString& url){
+QPair<QByteArray,QByteArray> RyRuleManager::getReplaceContent(QSharedPointer<RyRule> rule,const QString& url,bool *isResouceFound){
RyRuleReplaceContent rc(rule,url);
- return rc.getReplaceContent(_longCache);
+ return rc.getReplaceContent(_longCache,isResouceFound);
}
QString RyRuleManager::toJson(bool format)const{
View
2  rule/ryrulemanager.h
@@ -65,7 +65,7 @@ class RyRuleManager:public QObject{
//由于可能同时命中host及远程替换两种rule,此处需返回List
QList<QSharedPointer<RyRule> > getMatchRules(const QString& url);
//返回header body
- QPair<QByteArray,QByteArray> getReplaceContent(QSharedPointer<RyRule> rule,const QString& url="");
+ QPair<QByteArray,QByteArray> getReplaceContent(QSharedPointer<RyRule> rule,const QString& url="",bool *isResouceFound=0);
QString toJson(bool format=false)const;
// for Q+ offline-app
View
27 rule/ryrulereplacecontent.cpp
@@ -18,26 +18,27 @@ RyRuleReplaceContent::~RyRuleReplaceContent(){
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getReplaceContent(const QString& url,bool setLongCache){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getReplaceContent(const QString& url,bool setLongCache,bool *isResouceFound){
setUrl(url);
- return getReplaceContent(setLongCache);
+ return getReplaceContent(setLongCache,isResouceFound);
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getReplaceContent(bool setLongCache){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getReplaceContent(bool setLongCache,bool *isResouceFound){
switch(_rule->type()){
case RyRule::LOCAL_FILE_REPLACE:
- return getLocalReplaceContent(setLongCache);
+ return getLocalReplaceContent(setLongCache,isResouceFound);
break;
case RyRule::LOCAL_FILES_REPLACE:
- return getLocalMergeReplaceContent(setLongCache);
+ return getLocalMergeReplaceContent(setLongCache,isResouceFound);
break;
case RyRule::LOCAL_DIR_REPLACE:
- return getLocalDirReplaceContent(setLongCache);
+ return getLocalDirReplaceContent(setLongCache,isResouceFound);
break;
case RyRule::REMOTE_CONTENT_REPLACE:
- return getRemoteReplaceContent(setLongCache);
+ return getRemoteReplaceContent(setLongCache,isResouceFound);
break;
default:// error.
+ if(isResouceFound!=0)*isResouceFound = false;
qWarning()<<"this type of rule has no content to replace";
return QPair<QByteArray,QByteArray>(QByteArray("HTTP/1.1 404 NOT FOUND \r\nServer: Rythem\r\nContent-Length: 55\r\n\r\n"),
QByteArray("error rythem connot handle this type of replacement now"));
@@ -45,7 +46,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getReplaceContent(bool setLon
}
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getRemoteReplaceContent(bool ){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getRemoteReplaceContent(bool setLongCache,bool *isResouceFound){
QPair<QByteArray,QByteArray> ret;
QByteArray header,body;
@@ -62,6 +63,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getRemoteReplaceContent(bool
//bool isRequestDone = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).isValid();
if(!isOk){
body.append("<center>remote address unresponsable</center>");
+ if(isResouceFound!=0)*isResouceFound = false;
header.append(QString("HTTP/1.1 503 Serveice Unavailable\r\n"
"Server: Rythem \r\n"
"Content-Type: text/html \r\n"
@@ -92,7 +94,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getRemoteReplaceContent(bool
return ret;
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalReplaceContent(bool setLongCache){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalReplaceContent(bool setLongCache,bool *isResouceFound){
QPair<QByteArray,QByteArray> ret;
//open local file for read
QString status;
@@ -117,6 +119,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalReplaceContent(bool s
status = "200 OK";
body = file.readAll();
}else{
+ if(isResouceFound!=0)*isResouceFound = false;
status = "404 Not Found";
body.append(QString("file %1 not found").arg(replace));
}
@@ -139,7 +142,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalReplaceContent(bool s
return ret;
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalMergeReplaceContent(bool setLongCache){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalMergeReplaceContent(bool setLongCache,bool *isResouceFound){
QPair<QByteArray,QByteArray> ret;
//open local file for read
QString status;
@@ -196,6 +199,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalMergeReplaceContent(b
}
file.close();
if(mergeContentHasError){
+ if(isResouceFound!=0)*isResouceFound = false;
mimeType = "text/plain";
status = "404 NOT FOUND";
body.append(QString("merge file with wrong format:").append(replace).append(mergeFileContent));
@@ -230,7 +234,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalMergeReplaceContent(b
return ret;
}
-QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalDirReplaceContent(bool setLongCache){
+QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalDirReplaceContent(bool setLongCache,bool *isResouceFound){
QPair<QByteArray,QByteArray> ret;
//open local file for read
QString status;
@@ -316,6 +320,7 @@ QPair<QByteArray,QByteArray> RyRuleReplaceContent::getLocalDirReplaceContent(boo
status = "200 OK";
body = file.readAll();
}else{
+ if(isResouceFound!=0)*isResouceFound = false;
status = "404 Not Found";
body.append(QString("file:%1 not found").arg(fileName));
}
View
12 rule/ryrulereplacecontent.h
@@ -12,16 +12,16 @@ class RyRuleReplaceContent{
public:
RyRuleReplaceContent(QSharedPointer<RyRule> rule, const QString& url="");
~RyRuleReplaceContent();
- QPair<QByteArray,QByteArray> getReplaceContent(const QString& url,bool setLongCache=false);
- QPair<QByteArray,QByteArray> getReplaceContent(bool setLongCache=false);
+ QPair<QByteArray,QByteArray> getReplaceContent(const QString& url,bool setLongCache=false,bool *isResouceFound=0);
+ QPair<QByteArray,QByteArray> getReplaceContent(bool setLongCache=false,bool *isResouceFound=0);
void setUrl(const QString&);
void setRule(QSharedPointer<RyRule>);
private:
- QPair<QByteArray,QByteArray> getRemoteReplaceContent(bool setLongCache=false);
- QPair<QByteArray,QByteArray> getLocalReplaceContent(bool setLongCache=false);
- QPair<QByteArray,QByteArray> getLocalMergeReplaceContent(bool setLongCache=false);
- QPair<QByteArray,QByteArray> getLocalDirReplaceContent(bool setLongCache=false);
+ QPair<QByteArray,QByteArray> getRemoteReplaceContent(bool setLongCache=false,bool *isResouceFound=0);
+ QPair<QByteArray,QByteArray> getLocalReplaceContent(bool setLongCache=false,bool *isResouceFound=0);
+ QPair<QByteArray,QByteArray> getLocalMergeReplaceContent(bool setLongCache=false,bool *isResouceFound=0);
+ QPair<QByteArray,QByteArray> getLocalDirReplaceContent(bool setLongCache=false,bool *isResouceFound=0);
QNetworkAccessManager manager;
QString _url;
Please sign in to comment.
Something went wrong with that request. Please try again.