 Idea originating in 1950’s                 Standard way to get Input and Output                 A source or sink of da...
These are the people you can blame for the PHP streams implementation
 We will write code (in pairs) There will be a quiz (with prizes) You get out what you put in – participate!This is rea...
Abstracting I/O
 Access input and output generically Can write and read linearly May or may not be seekable Comes in chunks of data
 EVERYTHING include/require _once stream functions file system functions many other extensions
Stream         Contexts             Stream    StreamALL IO              Filter   Wrapper
   Stream    › Resource that exhibits a flow or succession of     data   Wrapper    › Tells a stream how to handle speci...
   Scheme    › The name of the wrapper to be used. file, http, https, ftp,      etc.   Target    › Depends on the wrappe...
 flock wrapper limitations non-existent pointers (infinite loops can  and will happen) error handling
 Parameters Options Modify or enhance a stream stream_context_set_param stream_context_set_option stream_context_cre...
Streams available by default
 file:// http:// ftp:// data:// glob://
   SSL                     Phar    ›   https://             › phar://    ›   ftps://             Zlib    ›   ssl://    ...
 Pipes STDIN, STDOUT, STDERR proc_open popen
 php://stdin php://stdout php://stderr php://output php://input php://filter (5.0.0) php://memory (5.1.0) php://te...
1.   Get a Feed from Flickr -     http://www.flickr.com/services/feeds/2. No curl installed, allow_url_fopen is on3. Displ...
Grab the feed as xml, pop it through simplexml, loop and display the output to a webpage
Userland Filters and Streams
 There are no interfaces Implement as though there were an  interface Seekable is optional Flushable is optional Dire...
 fopen               file_get_contentsInformation                 Return true or false                 $this->context ...
   fread                 fgets                 file_get_contentsInformation                 etc…                 Retu...
 fwrite               file_put_contentsInformation                 get in a string of data to deal with               ...
   feof                 file_get_contents                 freadInformation                 etc…                 Retur...
 fclose               file_get_contentsInformation                 Don’t return anything                 any cleanup s...
 fstat calls stream_stat EVERYTHING ELSE uses url_stat Good idea to do both   Return an array of data identical to sta...
 stream_seek stream_tell   stream_flush
 mkdir rmdir dir_closedir dir_opendir dir_readdir dir_rewinddir
 stream_lock stream_cast rename unlink
Create a custom wrapper for any kind of  network or shared memory storage you  want (except s3 which is way overdone)Imple...
Wrapper for wincache (btw, porting this to apc would be a breeze)Does basics + seek + flush (since we’re caching) + rename...
Use Case land – when streamsmake sense
 Data in s3 Data locally during development Easy switch out if alternative storage is  ever desired Storing image files
 Existing Zend Framework Code Register the s3:// wrapper Use a configuration setting for the stream  to use for all ima...
 Store and edit template files in a  database Have the snappiness of including from  disk Minimal Configuration
 db:// stream simple stream wrapper that looks for the  template in the db, and writes it to the  filesystem before retu...
 Talk to mercurial (hg binary) hg communicates via command line continue to pipe additional commands
 Use proc_open to keep a pipe to the  binary going Pass commands through stdin pipe as  necessary Abstract this out to ...
1. Name 3 Built in PHP streams.2. What is a Context? A Wrapper? A   Stream?3. How do you identify a stream?4. Name two ext...
 http://php.net/streams http://php.net/filesystem http://ciaranmcnulty.com/blog/2009/04/  simplifying-file-operations-u...
Because everyone needs to rot13a file they open on the fly
 Performs operations on stream data Can be prepended or appended (even  on the fly) Can be attached to read or write W...
 Data has an input and output state When reading in chunks, you may need  to cache in between reads to make  filters use...
   string filters    › string.rot13    › string.toupper    › string.tolower    › string.strip_tags
   convert filters    › convert.*       base64-encode       base64-decode       quoted-printable-encode       quoted-...
 bzip.compress and bzip.compress convert.iconv.* zlib.inflate and zlib.deflate mcrypt.* and mdecrypt.*
 Extend an internal class php_user_filter It’s not abstract… Yes that’s a horrible name Remember this pre-dates php 5....
 onCreate               basically a constructor               Called every time PHP needs a new filterInformation      ...
   php_user_filter    › $this->filtername    › $this->params    › $this->stream
 onClose               basically a destructorInformation                 no returnCode
   MUST return                  › PSFS_PASS_ON                  › PSFS_FEED_MEInformation       › PSFS_ERR_FATAL         ...
 $in and $out are “bucket brigades”  containing opaque “buckets” of data You can only touch buckets and  brigades with t...
 Given a list of bad words (we’ll use the 7  bad words), write a filter to remove them  from text Given a commonly missp...
 I only did the bad words one, and  created an extremely naïve regex that  stripped the “bad” words from whatever  text i...
1. What term is used to describe data   handling in filters?2. Name two built in filters you should be   using and why.3. ...
 http://php.net/streams http://php.net/filters
Putter about the network with me
   Socket    › Bidirectional network stream that speaks a      protocol   Transport    › Tells a network stream how to c...
 Network Stream, Network Transport,  Socket Transport Slightly different behavior from a file  stream Bi-directional data
   Sockets block    › stream_set_blocking    › stream_set_timeout    › stream_select feof means “connection_closed”? hu...
 New APIS in streams and filesystem  functions are replacements Extension is old and not really kept up to  date (bit ro...
 tcp udp unix udg SSL extension    ›   ssl    ›   sslv2    ›   sslv3    ›   tls
 Given two files of quotes, write a server  with PHP streams methods to return  random quotes, optionally allow client to...
 socket_server.php – uses  stream_socket_server and  stream_accept socket_client.php – uses  stream_socket_client and a ...
1. How is a socket different from a stream?2. What transports does PHP provide by   default?3. Name the two most commonly ...
 http://php.net/streams http://php.net/transports http://wikipedia.org/wiki/Unix_domain_s  ocket http://tools.ietf.org...
 Set of interfaces for streams Improved based filter class Wrappers for chmod and touch More tests! Any you have?
 http://emsmith.net http://joind.in/3439 auroraeosrose@gmail.com IRC – freenode – auroraeosrose #php-gtk #coapp
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Writing and using php streams and sockets   tek11
Upcoming SlideShare
Loading in …5
×

Writing and using php streams and sockets tek11

2,964 views

Published on

Given at php tek 2

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,964
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Writing and using php streams and sockets tek11

  1. 1.  Idea originating in 1950’s  Standard way to get Input and Output  A source or sink of dataDefinitions  C – stdin, stderr, stdout  C++ iostreamWho uses them  Perl IO  Python io  Java  C#
  2. 2. These are the people you can blame for the PHP streams implementation
  3. 3.  We will write code (in pairs) There will be a quiz (with prizes) You get out what you put in – participate!This is really 3 talks in one - Streams Filters Sockets
  4. 4. Abstracting I/O
  5. 5.  Access input and output generically Can write and read linearly May or may not be seekable Comes in chunks of data
  6. 6.  EVERYTHING include/require _once stream functions file system functions many other extensions
  7. 7. Stream Contexts Stream StreamALL IO Filter Wrapper
  8. 8.  Stream › Resource that exhibits a flow or succession of data Wrapper › Tells a stream how to handle specific protocols and encodings Context › A set of parameters and options to tell a stream (or filter) how to behave
  9. 9.  Scheme › The name of the wrapper to be used. file, http, https, ftp, etc. Target › Depends on the wrapper, filesystem uses a string path name, ssh2 uses a PHP resource home/bar/foo.txt file:///home/bar/foo.txt http://www.example.com/foo.txt ftp://user:pass@ftp.example.com/foo.txt php://filter/read=string.toupper|string.rot13/resource =http://www.example.com
  10. 10.  flock wrapper limitations non-existent pointers (infinite loops can and will happen) error handling
  11. 11.  Parameters Options Modify or enhance a stream stream_context_set_param stream_context_set_option stream_context_create
  12. 12. Streams available by default
  13. 13.  file:// http:// ftp:// data:// glob://
  14. 14.  SSL  Phar › https:// › phar:// › ftps://  Zlib › ssl:// › compress.zlib:// › tls:// › zlib:// SSH  Bzip › ssh2.shell:// › compress.bz2:// › ssh2.exec:// › ssh2.tunnel:// › ssh2.sftp:// › ssh2.scp://
  15. 15.  Pipes STDIN, STDOUT, STDERR proc_open popen
  16. 16.  php://stdin php://stdout php://stderr php://output php://input php://filter (5.0.0) php://memory (5.1.0) php://temp (5.1.0)
  17. 17. 1. Get a Feed from Flickr - http://www.flickr.com/services/feeds/2. No curl installed, allow_url_fopen is on3. Display however you would like
  18. 18. Grab the feed as xml, pop it through simplexml, loop and display the output to a webpage
  19. 19. Userland Filters and Streams
  20. 20.  There are no interfaces Implement as though there were an interface Seekable is optional Flushable is optional Directory support is optional
  21. 21.  fopen  file_get_contentsInformation  Return true or false  $this->context will have any context metadataCode
  22. 22.  fread  fgets  file_get_contentsInformation  etc…  Return string data or false  $this->context will have any context metadataCode
  23. 23.  fwrite  file_put_contentsInformation  get in a string of data to deal with  return how many bytes you wroteCode
  24. 24.  feof  file_get_contents  freadInformation  etc…  Return true or false  $this->context will have any context metadataCode
  25. 25.  fclose  file_get_contentsInformation  Don’t return anything  any cleanup should go hereCode
  26. 26.  fstat calls stream_stat EVERYTHING ELSE uses url_stat Good idea to do both Return an array of data identical to stat()
  27. 27.  stream_seek stream_tell stream_flush
  28. 28.  mkdir rmdir dir_closedir dir_opendir dir_readdir dir_rewinddir
  29. 29.  stream_lock stream_cast rename unlink
  30. 30. Create a custom wrapper for any kind of network or shared memory storage you want (except s3 which is way overdone)Implement at least the basics – extra points for more features
  31. 31. Wrapper for wincache (btw, porting this to apc would be a breeze)Does basics + seek + flush (since we’re caching) + rename + unlinkNo directories
  32. 32. Use Case land – when streamsmake sense
  33. 33.  Data in s3 Data locally during development Easy switch out if alternative storage is ever desired Storing image files
  34. 34.  Existing Zend Framework Code Register the s3:// wrapper Use a configuration setting for the stream to use for all images on the system
  35. 35.  Store and edit template files in a database Have the snappiness of including from disk Minimal Configuration
  36. 36.  db:// stream simple stream wrapper that looks for the template in the db, and writes it to the filesystem before returning the data The cached location is FIRST in the include path, so if it fails, the db stream gets hit
  37. 37.  Talk to mercurial (hg binary) hg communicates via command line continue to pipe additional commands
  38. 38.  Use proc_open to keep a pipe to the binary going Pass commands through stdin pipe as necessary Abstract this out to other binaries that are used by the system
  39. 39. 1. Name 3 Built in PHP streams.2. What is a Context? A Wrapper? A Stream?3. How do you identify a stream?4. Name two extensions that provide additional PHP streams.
  40. 40.  http://php.net/streams http://php.net/filesystem http://ciaranmcnulty.com/blog/2009/04/ simplifying-file-operations-using-php- stream-wrappers
  41. 41. Because everyone needs to rot13a file they open on the fly
  42. 42.  Performs operations on stream data Can be prepended or appended (even on the fly) Can be attached to read or write When a filter is added for read and write, two instances of the filter are created.
  43. 43.  Data has an input and output state When reading in chunks, you may need to cache in between reads to make filters useful Use the right tool for the job
  44. 44.  string filters › string.rot13 › string.toupper › string.tolower › string.strip_tags
  45. 45.  convert filters › convert.*  base64-encode  base64-decode  quoted-printable-encode  quoted-printable-decode dechunk › decode remote HTTP chunked encoding streams consumed › eats data (that’s all it does)
  46. 46.  bzip.compress and bzip.compress convert.iconv.* zlib.inflate and zlib.deflate mcrypt.* and mdecrypt.*
  47. 47.  Extend an internal class php_user_filter It’s not abstract… Yes that’s a horrible name Remember this pre-dates php 5.0 decisions
  48. 48.  onCreate  basically a constructor  Called every time PHP needs a new filterInformation (on every stream)  return true or falseCode
  49. 49.  php_user_filter › $this->filtername › $this->params › $this->stream
  50. 50.  onClose  basically a destructorInformation  no returnCode
  51. 51.  MUST return › PSFS_PASS_ON › PSFS_FEED_MEInformation › PSFS_ERR_FATAL  You get buckets of data and do stuff to themCode
  52. 52.  $in and $out are “bucket brigades” containing opaque “buckets” of data You can only touch buckets and brigades with the stream_bucket_* functions You get a bucket using stream_bucket_make_writeable
  53. 53.  Given a list of bad words (we’ll use the 7 bad words), write a filter to remove them from text Given a commonly misspelled word, fix the spelling in the text
  54. 54.  I only did the bad words one, and created an extremely naïve regex that stripped the “bad” words from whatever text it finds. It buffers the entirety of the text before doing the replacement – this could be done by looking for word boundaries and doing it piecemeal to improve performance
  55. 55. 1. What term is used to describe data handling in filters?2. Name two built in filters you should be using and why.3. What is the default mode filters are appended with?4. What is the difference between appending and prepending a filter?
  56. 56.  http://php.net/streams http://php.net/filters
  57. 57. Putter about the network with me
  58. 58.  Socket › Bidirectional network stream that speaks a protocol Transport › Tells a network stream how to communicate Wrapper › Tells a stream how to handle specific protocols and encodings
  59. 59.  Network Stream, Network Transport, Socket Transport Slightly different behavior from a file stream Bi-directional data
  60. 60.  Sockets block › stream_set_blocking › stream_set_timeout › stream_select feof means “connection_closed”? huge reads or writes (think 8K) stream_get_meta_data is READ ONLY
  61. 61.  New APIS in streams and filesystem functions are replacements Extension is old and not really kept up to date (bit rot) Extension is more low level stream_socket_server stream_socket_client
  62. 62.  tcp udp unix udg SSL extension › ssl › sslv2 › sslv3 › tls
  63. 63.  Given two files of quotes, write a server with PHP streams methods to return random quotes, optionally allow client to pick which file to retrieve quotes from Given a simple tcp server that will return a random quote – make a client that pulls quotes for users
  64. 64.  socket_server.php – uses stream_socket_server and stream_accept socket_client.php – uses stream_socket_client and a bit of loop magic to make a little “cli” app
  65. 65. 1. How is a socket different from a stream?2. What transports does PHP provide by default?3. Name the two most commonly used stream socket functions.4. How can a user add additional transports to PHP?
  66. 66.  http://php.net/streams http://php.net/transports http://wikipedia.org/wiki/Unix_domain_s ocket http://tools.ietf.org/html/rfc1122
  67. 67.  Set of interfaces for streams Improved based filter class Wrappers for chmod and touch More tests! Any you have?
  68. 68.  http://emsmith.net http://joind.in/3439 auroraeosrose@gmail.com IRC – freenode – auroraeosrose #php-gtk #coapp

×