Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9370f96

Browse files
committed
step by step getting there to partial output presentation to restful API (issue #297), not quite yet though..
1 parent b55555e commit 9370f96

5 files changed

Lines changed: 24 additions & 11 deletions

File tree

lib/core/common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ def goGoodSamaritan(prevValue, originalCharset):
17711771
else:
17721772
return None, None, None, originalCharset
17731773

1774-
def getPartRun():
1774+
def getPartRun(alias=True):
17751775
"""
17761776
Goes through call stack and finds constructs matching conf.dbmsHandler.*.
17771777
Returns it or its alias used in txt/common-outputs.txt
@@ -1803,7 +1803,10 @@ def getPartRun():
18031803
pass
18041804

18051805
# Return the INI tag to consider for common outputs (e.g. 'Databases')
1806-
return commonPartsDict[retVal][1] if isinstance(commonPartsDict.get(retVal), tuple) else retVal
1806+
if alias:
1807+
return commonPartsDict[retVal][1] if isinstance(commonPartsDict.get(retVal), tuple) else retVal
1808+
else:
1809+
return retVal
18071810

18081811
def getUnicode(value, encoding=None, system=False, noneToNull=False):
18091812
"""

lib/techniques/blind/inference.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ def bisection(payload, expression, length=None, charsetType=None, firstChar=None
8989
try:
9090
# Set kb.partRun in case "common prediction" feature (a.k.a. "good
9191
# samaritan") is used or the engine is called from the API
92-
kb.partRun = getPartRun() if conf.predictOutput or hasattr(conf, "api") else None
92+
if conf.predictOutput:
93+
kb.partRun = getPartRun()
94+
elif hasattr(conf, "api"):
95+
kb.partRun = getPartRun(alias=False)
96+
else:
97+
kb.partRun = None
9398

9499
if partialValue:
95100
firstChar = len(partialValue)

lib/techniques/error/use.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def errorUse(expression, dump=False):
245245
_, _, _, _, _, expressionFieldsList, expressionFields, _ = agent.getFields(expression)
246246

247247
# Set kb.partRun in case the engine is called from the API
248-
kb.partRun = getPartRun() if hasattr(conf, "api") else None
248+
kb.partRun = getPartRun(alias=False) if hasattr(conf, "api") else None
249249

250250
# We have to check if the SQL query might return multiple entries
251251
# and in such case forge the SQL limiting the query output one

lib/techniques/union/use.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def unionUse(expression, unpack=True, dump=False):
165165
_, _, _, _, _, expressionFieldsList, expressionFields, _ = agent.getFields(origExpr)
166166

167167
# Set kb.partRun in case the engine is called from the API
168-
kb.partRun = getPartRun() if hasattr(conf, "api") else None
168+
kb.partRun = getPartRun(alias=False) if hasattr(conf, "api") else None
169169

170170
if expressionFieldsList and len(expressionFieldsList) > 1 and "ORDER BY" in expression.upper():
171171
# Removed ORDER BY clause because UNION does not play well with it

lib/utils/api.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,26 @@ def write(self, value, status=CONTENT_STATUS.IN_PROGRESS, content_type=None):
178178
if content_type is None:
179179
content_type = 99
180180

181+
output = conf.database_cursor.execute("SELECT id, value FROM data WHERE taskid = ? AND status = ? AND content_type = ? LIMIT 0,1",
182+
(self.taskid, status, content_type))
183+
181184
if status == CONTENT_STATUS.IN_PROGRESS:
182-
output = conf.database_cursor.execute("SELECT id, value FROM data WHERE taskid = ? AND status = ? AND content_type = ? LIMIT 0,1",
183-
(self.taskid, status, content_type))
185+
# Ignore all non-relevant messages
186+
if kb.partRun is None:
187+
return
184188

185189
if len(output) == 0:
186190
conf.database_cursor.execute("INSERT INTO data VALUES(NULL, ?, ?, ?, ?)",
187191
(self.taskid, status, content_type, jsonize(value)))
188192
else:
189-
new_value = "%s%s" % (output[0][1], value)
193+
new_value = "%s%s" % (dejsonize(output[0][1]), value)
190194
conf.database_cursor.execute("UPDATE data SET value = ? WHERE id = ?",
191195
(jsonize(new_value), output[0][0]))
192196
else:
197+
if len(output) > 0:
198+
conf.database_cursor.execute("DELETE FROM data WHERE taskid = ? AND status = %s AND content_type = ?" % CONTENT_STATUS.IN_PROGRESS,
199+
(self.taskid, content_type))
200+
193201
conf.database_cursor.execute("INSERT INTO data VALUES(NULL, ?, ?, ?, ?)",
194202
(self.taskid, status, content_type, jsonize(value)))
195203
else:
@@ -217,9 +225,6 @@ def emit(self, record):
217225

218226
def setRestAPILog():
219227
if hasattr(conf, "api"):
220-
#conf.database_connection = sqlite3.connect(conf.database, timeout=1, isolation_level=None)
221-
#conf.database_cursor = conf.database_connection.cursor()
222-
223228
conf.database_cursor = Database(conf.database)
224229
conf.database_cursor.connect("client")
225230

0 commit comments

Comments
 (0)