Skip to content

Commit

Permalink
Merge pull request #109 from VTopoliuk/pcurl
Browse files Browse the repository at this point in the history
pcurl command
  • Loading branch information
kastiglione committed Sep 28, 2015
2 parents ed8ad40 + a231b3b commit 033def7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
66 changes: 66 additions & 0 deletions commands/FBPrintCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def lldbcommands():
FBPrintData(),
FBPrintTargetActions(),
FBPrintJSON(),
FBPrintAsCurl(),
]

class FBPrintViewHierarchyCommand(fb.FBCommand):
Expand Down Expand Up @@ -450,3 +451,68 @@ def run(self, arguments, options):

print jsonString

class FBPrintAsCurl(fb.FBCommand):
def name(self):
return 'pcurl'

def description(self):
return 'Print the NSURLRequest (HTTP) as curl command.'

def options(self):
return [
fb.FBCommandArgument(short='-e', long='--embed-data', arg='embed', boolean=True, default=False, help='Embed request data as base64.'),
]

def args(self):
return [ fb.FBCommandArgument(arg='request', type='NSURLRequest*/NSMutableURLRequest*', help='The request to convert to the curl command.') ]

def generateTmpFilePath(self):
return '/tmp/curl_data_{}'.format(fb.evaluateExpression('(NSTimeInterval)[NSDate timeIntervalSinceReferenceDate]'))

def run(self, arguments, options):
request = arguments[0]
HTTPHeaderSring = ''
HTTPMethod = fb.evaluateExpressionValue('(id)[{} HTTPMethod]'.format(request)).GetObjectDescription()
URL = fb.evaluateExpressionValue('(id)[{} URL]'.format(request)).GetObjectDescription()
timeout = fb.evaluateExpression('(NSTimeInterval)[{} timeoutInterval]'.format(request))
HTTPHeaders = fb.evaluateObjectExpression('(id)[{} allHTTPHeaderFields]'.format(request))
HTTPHeadersCount = fb.evaluateIntegerExpression('[{} count]'.format(HTTPHeaders))
allHTTPKeys = fb.evaluateObjectExpression('[{} allKeys]'.format(HTTPHeaders))
for index in range(0, HTTPHeadersCount):
key = fb.evaluateObjectExpression('[{} objectAtIndex:{}]'.format(allHTTPKeys, index))
keyDescription = fb.evaluateExpressionValue('(id){}'.format(key)).GetObjectDescription()
value = fb.evaluateExpressionValue('(id)[(id){} objectForKey:{}]'.format(HTTPHeaders, key)).GetObjectDescription()
if len(HTTPHeaderSring) > 0:
HTTPHeaderSring += ' '
HTTPHeaderSring += '-H "{}: {}"'.format(keyDescription, value)
HTTPData = fb.evaluateObjectExpression('[{} HTTPBody]'.format(request))
dataFile = None
dataAsString = None
if fb.evaluateIntegerExpression('[{} length]'.format(HTTPData)) > 0:
if options.embed:
if fb.evaluateIntegerExpression('[{} respondsToSelector:@selector(base64EncodedStringWithOptions:)]'.format(HTTPData)):
dataAsString = fb.evaluateExpressionValue('(id)[(id){} base64EncodedStringWithOptions:0]'.format(HTTPData)).GetObjectDescription()
else :
print 'This version of OS doesn\'t supports base64 data encoding'
return False
elif not runtimeHelpers.isIOSDevice():
dataFile = self.generateTmpFilePath()
if not fb.evaluateBooleanExpression('(BOOL)[{} writeToFile:@"{}" atomically:NO]'.format(HTTPData, dataFile)):
print 'Can\'t write data to file {}'.format(dataFile)
return False
else:
print 'HTTPBody data for iOS Device is supported only with "--embed-data" flag'
return False

commandString = ''
if dataAsString is not None and len(dataAsString) > 0:
dataFile = self.generateTmpFilePath()
commandString += 'echo "{}" | base64 -D -o "{}" && '.format(dataAsString, dataFile)
commandString += 'curl -X {} --connect-timeout {}'.format(HTTPMethod, timeout)
if len(HTTPHeaderSring) > 0:
commandString += ' ' + HTTPHeaderSring
if dataFile is not None:
commandString += ' --data-binary @"{}"'.format(dataFile)

commandString += ' "{}"'.format(URL)
print commandString
6 changes: 6 additions & 0 deletions fblldbobjcruntimehelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ def isMacintoshArch():
command = '(void*)objc_getClass("{}")'.format(nsClassName)

return (fb.evaluateBooleanExpression(command + '!= nil'))

def isIOSSimulator():
return fb.evaluateExpressionValue('(id)[[UIDevice currentDevice] model]').GetObjectDescription().lower().find('simulator') >= 0

def isIOSDevice():
return not isMacintoshArch() and not isIOSSimulator()

0 comments on commit 033def7

Please sign in to comment.