Slide 1

Slide 1 text

Proxy  Server

Slide 2

Slide 2 text

Hi,  I’m  Zeeshan @zeeshan_khan

Slide 3

Slide 3 text

Problem

Slide 4

Slide 4 text

Problems  /  Features

Slide 5

Slide 5 text

Problems  /  Features • Client  Demo

Slide 6

Slide 6 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity

Slide 7

Slide 7 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields

Slide 8

Slide 8 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields • Large  Image  /  Video  Uploads

Slide 9

Slide 9 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields • Large  Image  /  Video  Uploads • Data  Formatting  /  Validation

Slide 10

Slide 10 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields • Large  Image  /  Video  Uploads • Data  Formatting  /  Validation • Random  Error  Probability

Slide 11

Slide 11 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields • Large  Image  /  Video  Uploads • Data  Formatting  /  Validation • Random  Error  Probability • HUD,  Progress  Bar,  UI  Refreshing

Slide 12

Slide 12 text

Problems  /  Features • Client  Demo • Slow  Network  Connectivity • Mandatory  Fields • Large  Image  /  Video  Uploads • Data  Formatting  /  Validation • Random  Error  Probability • HUD,  Progress  Bar,  UI  Refreshing • Session  Setup  etc…

Slide 13

Slide 13 text

Solution

Slide 14

Slide 14 text

Solution Any  Ideas?

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Proxy  Server

Slide 17

Slide 17 text

Proxy  Server localhost  /  127.0.0.1

Slide 18

Slide 18 text

How  to  configure? • HTTP  Server  Communication – Start  Server  -­‐  Open  a  Socket – Receive  Incoming  Connections – Response  Handling – Stop  Server  -­‐  Close  Socket

Slide 19

Slide 19 text

Open  a  Socket  I • Create  Socket  object CFSocketRef  socket  =  CFSocketCreate(kCFAllocatorDefault,  PF_INET,   SOCK_STREAM,  IPPROTO_TCP,  0,  NULL,  NULL); • Get  Native  Socket int  reuse  =  true; int  fileDescriptor  =  CFSocketGetNative(socket); setsockopt(fileDescriptor,  SOL_SOCKET,  SO_REUSEADDR,  (void  *)&reuse,   sizeof(int));

Slide 20

Slide 20 text

Open  a  Socket  II • Bind  Socket  to  address struct  sockaddr_in  address; memset(&address,  0,  sizeof(address)); address.sin_len  =  sizeof(address); address.sin_family  =  AF_INET; address.sin_addr.s_addr  =  htonl(INADDR_ANY); NSInteger  portNumber  =  8080; address.sin_port  =  htons(portNumber); CFDataRef  addressData  =  CFDataCreate(NULL,  (const  UInt8  *)&address,   sizeof(address)); CFSocketSetAddress(socket,  addressData);

Slide 21

Slide 21 text

Receive  Incoming  Connections  I • Constructing  File  Handler NSFileHandle  *listeningHandle  =  [[NSFileHandle  alloc]   initWithFileDescriptor:fileDescriptor  closeOnDealloc:YES]; [listeningHandle  acceptConnectionInBackgroundAndNotify]; • Attach  Connection  Listener [[NSNotificationCenter  defaultCenter]  addObserver:self   selector:@selector(receiveIncomingConnectionNotification:)   name:NSFileHandleConnectionAcceptedNotification  object:nil];

Slide 22

Slide 22 text

Receive  Incoming  Connections  II • Receive  File  Handler NSDictionary  *userInfo  =  [notification  userInfo]; NSFileHandle  *incomingFileHandle  =  [userInfo   objectForKey:NSFileHandleNotificationFileHandleItem]; • Attach  Incoming  Data  Listener [[NSNotificationCenter  defaultCenter]  addObserver:self   selector:@selector(receiveIncomingDataNotification:)   name:NSFileHandleDataAvailableNotification   object:incomingFileHandle];  

Slide 23

Slide 23 text

Two  File  Handlers  I • Connection  Listening   – Manually  Created  Once  for  Socket,  which  listens  to   new  connections. • Data  Listening   – Automatically  Created  for  each  new  connection,   which  listens  to  new  available  data.

Slide 24

Slide 24 text

Two  File  Handlers  II Opened  Socket Broadcasting   Connections Your  Listener  /   Connection   Handler.  A  Receiver  for   incoming   connections Each   Connection’s   Data  Handler   given  by   Connection   Handler Available  Data   from   Notification   Object  Comes   from  Data   Handler

Slide 25

Slide 25 text

Receive  Incoming  Data  I • Handle  Incoming  Data NSFileHandle  *incomingFileHandle  =  [notification  object]; NSData  *data  =  [incomingFileHandle  availableData]; NSString  *strIncomingData  =  [[[NSString  alloc]  initWithData:data  encoding:NSUTF8StringEncoding]  autorelease]; • Create  HTTP  Message  to  store  date CFHTTPMessageRef  incomingRequest  =   CFHTTPMessageCreateEmpty(kCFAllocatorDefault,  TRUE); CFHTTPMessageAppendBytes(incomingRequest,  [data  bytes],  [data  length]); CFHTTPMessageIsHeaderComplete(incomingRequest);

Slide 26

Slide 26 text

Receive  Incoming  Data  II • Sample  Incoming  Data GET  /  HTTP/1.1 Host:  127.0.0.1:8080 Accept-­‐Encoding:  gzip,  deflate Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,/*;q=0.8 User-­‐Agent:  Mozilla/5.0  (Macintosh;  Intel  Mac  OS  X  10_10)  AppleWebKit/538.34.48   (KHTML,  like  Gecko)  Version/8.0  Safari/538.35.8 Accept-­‐Language:  en-­‐us DNT:  1 Connection:  keep-­‐alive

Slide 27

Slide 27 text

Handling  Response  I • Parsing  Request  Data NSURL  *requestURL  =  NSMakeCollectable(CFHTTPMessageCopyRequestURL(ref)); NSString  *httpMethod  =   NSMakeCollectable(CFHTTPMessageCopyRequestMethod(ref)); NSDictionary  *httpHeaderFields  =   NSMakeCollectable(CFHTTPMessageCopyAllHeaderFields(ref)); NSData  *httpBody  =  NSMakeCollectable(CFHTTPMessageCopyBody(ref));

Slide 28

Slide 28 text

Handling  Response  II • Create  HTTP  Response NSInteger  responseCode  =  200; CFHTTPMessageRef  response  =   CFHTTPMessageCreateResponse(kCFAllocatorDefault,  responseCode,  NULL,   kCFHTTPVersion1_1); • Add  Response  header  fields CFHTTPMessageSetHeaderFieldValue(response,  (CFStringRef)@"Content-­‐Type",   (CFStringRef)@"text/plain"); CFHTTPMessageSetHeaderFieldValue(response,  (CFStringRef)@"Connection",   (CFStringRef)@"close");

Slide 29

Slide 29 text

Handling  Response  III • Add  Data  Length  in  header  fields NSString  *dataLength  =  [NSString  stringWithFormat:@”%d",   fileData.length]; CFHTTPMessageSetHeaderFieldValue(response,  (CFStringRef)@"Content-­‐ Length",  (CFStringRef)dataLength); • Serialize  HTTP  Response  in  Data CFDataRef  headerData  =  CFHTTPMessageCopySerializedMessage(response);

Slide 30

Slide 30 text

Sending  Response • Write  HTTP  Header  Data  and  Response  Data [incomingFileHandle  writeData:(NSData*)headerData]; [incomingFleHandle  writeData:fileData]; • Close  file  handler  and  Remove  Listeners [incomingFileHandle  closeFile]; [[NSNotificationCenter  defaultCenter]  removeObserver:self name:NSFileHandleDataAvailableNotification  object:incomingFileHandle];

Slide 31

Slide 31 text

Stop  Server • Remove  Connection  Listener [[NSNotificationCenter  defaultCenter]  removeObserver:self   name:NSFileHandleConnectionAcceptedNotification  object:nil]; • Close  Socket [listeningHandle  closeFile]; CFSocketInvalidate(socket); CFRelease(socket);

Slide 32

Slide 32 text

Configure  in  App  (swift  syntax  J) • Start  Server func  application(application:  UIApplication!,  didFinishLaunchingWithOptions   launchOptions:  NSDictionary!)  -­‐>  Bool func  applicationWillEnterForeground(application:  UIApplication!) • Stop  Server func  applicationDidEnterBackground(application:  UIApplication!)

Slide 33

Slide 33 text

Solutions • Set  up  Core  Data  or  SQLite  for  dynamic  tables • Add  delay  before  Response  Handling • Configure  Error  Probability  On  Response • Configure  Session  Setup • Add  Server  States  (Idle  /  Running)   • Add  Configurations  like  Port  no,  etc…  

Slide 34

Slide 34 text

Frameworks  Needed • #import   – For  #defines  like  AF_INET  protocol  family • #import   – Socket  address  structure  and  others  defined • #import   – Core  Network  APIs  (need  to  add  into  project)

Slide 35

Slide 35 text

Demo

Slide 36

Slide 36 text

References • http://www.cocoawithlove.com/2009/07/simple-­‐ extensible-­‐http-­‐server-­‐in-­‐cocoa.html • https://github.com/robbiehanson/CocoaHTTPServer • https://developer.apple.com/library/ios/technotes/ tn2277/_index.html • Google  for  Cocoa  HTTP  Server

Slide 37

Slide 37 text

Any  Questions?

Slide 38

Slide 38 text

Thanks! Say  Hi  sometimes  J @zeeshan_khan