Traffic Server APIs Brian Geffon LinkedIn Nov 16, 2015.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Kit Chan ATS Lua Plugin Kit Chan Hi, My name is kit.
Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Windows Communication Foundation and Web Services.
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Web Services 101 James Payne Managing Director for New Media / Advancement July 30, 2013.
ZFApp Preview Walkthrough. What is ZFApp? ZFApp is an application framework built on top of Zend Framework Fully compatible with the latest ZF Versions.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Object-Oriented Programming in C++
Defining New Types Lecture 21 Hartmut Kaiser
JSP Filters 23-Oct-15. JSP - FILTERS A filter is an object that can transform a request or modify a response. Filters are not servlets; they don't actually.
Win32 Programming Lesson 18: More Memory Mapped Files and the HEAP (Finally, cool stuff!)
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Everything Is an Object Manipulate objects with references The identifier you manipulate is actually a “reference” to an object. Like a television.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Lesson 6 – Libraries & APIs Libraries & APIs. Objective: We will explore how to take advantage of the huge number of pre-made classes provided with Java.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
Secure Operating Systems Lesson F: Capability Based Systems.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
FIT Objectives By the end of this lecture, students should: understand the role of constructors understand how non-default constructors are.
Chapter 4 Request and Response. Servlets are controlled by the container.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Windows Communication Foundation and Web Services
Memory Management.
CSE691 Software Models and Analysis.
Essentials of UrbanCode Deploy v6.1 QQ147
CS 215 Final Review Ismail abumuhfouz Fall 2014.
z/Ware 2.0 Technical Overview
Windows Communication Foundation and Web Services
The Object-Oriented Thought Process Chapter 05
March 17, 1999 Judy Slein WebDAV Collections March 17, 1999 Judy Slein
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CSE 333 – Section 5 C++ (1m).
Restful APIs 101 Laura
Introduction to Classes and Objects
Presentation transcript:

Traffic Server APIs Brian Geffon LinkedIn Nov 16, 2015

C++ API: Introduction Why was a C++ API necessary?

C++ API Design Considerations It should never require ts.h – Encapsulation is used heavily to prevent exposing ts.h types and functions – For the most part it’s pretty independent of TS versions, we even considered making it standalone from the core but then we’d have #ifdef’s everywhere. Given the stability of atscppapi it’s probably completely unnecessary at this point. – Unfortunately because of transaction level configs ( TSHttpTxnConfig* ) we now have #include Can be removed with a build time step to generate the equivalent C++ enums (anyone interested in taking this on?)

C++ API Just a wrapper on top of the C API – Most hooks and transaction level methods available to C API (pull requests appreciated for additional functionality) – Because it just wraps the C API you can actually use them together if you wanted to. C++ classes abstract away the weird details of the C API, why should it require 10+ lines of code to get a header value?

C++ API C plugins can sometimes be tricky to get right. – QUIZ: Can anyone tell when you REALLY need to use TSHandleMLocHandle release? – Sometimes you’re responsible for freeing the returned strings ( TSUrlStringGet, TSHttpTxnGetClientPequestBody, etc.. ) – Sometimes you’re responsible for heap allocating some strings TSHttpTxnErrorBodySet

C++ API C plugins can sometimes by tricky to get right. – Transformations What a pain these can be Transformation plugins have a tremendous amount of boiler plate code. – Intercepts Intercepts also have a huge amount of complexity due to setup and Vconn reading / writing, etc. – Cleanup in general

Mapping C to C++ API In the C API you have Global Hooks: hooks that are applied to every transaction. You also have Transaction Hooks: hooks that are applied only to a specific transaction. – Transformations are technically “Transaction Hooks” in that they only apply to a single transaction. – Intercepts can also be thought of as “Transaction Hooks”

Global / Transaction “Hooks” In the C++ API world we refer to these concepts as “Plugins”: A plugin has many hooks. Plugin GlobalPlugin TransactionPlugin TransformationPlugin InterceptPlugin

Plugins: Where do you fit in? Plugin GlobalPlugin TransactionPlugin TransformationPlugin InterceptPlugin Gzip(Inflate/Deflate)Plugin MyGlobalPlugin MyTransformationPlugin MyTransactionPlugin

Global Plugin Example Registration You have the same DSO entry point as C plugins That looks suspiciously like a memory leak Global plugins exist for the life of the process.

Global Plugin Example Creating a Hook As we’ve shown all you do is extend the GlobalPlugin class. Your constructor is where you register the hooks you’re interested in. Remember: you always extend the class for the plugin type you’re trying to implement In your constructor you’ll add your Hooks Finally add the callbacks for each of your hooks.

Global Plugin Example Creating a Hook As we’ve shown all you do is extend the GlobalPlugin class. Your constructor is where you register the hooks you’re interested in. NOTE: A very common gotcha is misspelling or using the wrong callback. The default implementation for all hooks is to simply resume the transaction! NOTE: A very common gotcha is misspelling or using the wrong callback. The default implementation for all hooks is to simply resume the transaction! Always remember to reenable (resume) the transaction or you can reenable to an error state.

Transaction Plugins Transaction plugins and Global Plugins are basically the same in terms of implementation. – Transaction scoped storage is free! The parent constructor handles the transaction setup This class is automatically destroyed when a Transaction goes out of scope, so you can cleanup in your destructor. Free transaction scoped storage.

Transactions: attaching transaction plugins Global hooks can be registered at anytime but they are typically registered in TSPluginInit TransactionPlugins are attached to a Transaction object. Just attach a new instance of a TransactionPlugin the transaction takes ownership of the TransactionPlugin and will ensure it’s deleted.

Transactions: request / response objects You have six request / response objects that you can access via Transactions ClientRequest &getClientRequest() Request &getServerRequest() Response &getServerResponse() Response &getClientResponse() Request &getCachedRequest() Response &getCachedResponse() Why do client requests return a ClientRequest object instead of just a Request? ClientRequest Extends Request Because of Pristine urls. As we will see soon Request objects have URL objects, in the case of client requests we actually have the pristine url too. Why do client requests return a ClientRequest object instead of just a Request? ClientRequest Extends Request Because of Pristine urls. As we will see soon Request objects have URL objects, in the case of client requests we actually have the pristine url too.

Request Objects Request objects allow you to access (see Request.h ) – Request URL (Returns a URL object) Pristine URL is also available if it’s a ClientRequest – Headers (Returns a Headers object) – Method – HTTP Version

URL Objects You can obtain a URL object from a Transaction (transaction.getUrl())

Headers Objects Behave like standard library containers (more or less) Content-Type: foo Content-Encoding: gzip, sdch Content-Length: 20 X-Some-Header: 123,blah X-Another-Header: foo, bar, baz A instance of the Headers class contains all the HeaderFields: a header field is a “line.” Iterators are exposed to allow you to iterate over the Headers object, it’s a header_field_iterator.

HeaderField Objects Content-Type: foo Content-Encoding: gzip, sdch Content-Length: 20 X-Some-Header: 123,blah X-Another-Header: foo, bar, baz A HeaderField contains a HeaderFieldName and many std::string values.

HeaderFieldName Objects Content-Type: foo Content-Encoding: gzip, sdch Content-Length: 20 X-Some-Header: 123,blah X-Another-Header: foo, bar, baz HeaderFieldName is a class created to handle the fact that header names are case-insensitive. It has overloaded comparison operators for case insensitive comparisons. Every HeaderField has a HeaderFieldName

Header values Content-Type: foo Content-Encoding: gzip, sdch Content-Length: 20 X-Some-Header: 123,blah X-Another-Header: foo, bar, baz A HeaderField contains many header values which are std::strings. To behave like standard containers there are iterators exposed, it’s a header_field_value_iterator. Every HeaderField has many values (std::string)

Transaction Examples This very simple example looks at the response code from the server and if it’s not 200 it will set a header. NOTE: As is the behavior with most standard containers operator[] causes the element to be created if it doesn’t exist. In the next example we’ll use find on a Header object.

Transaction Examples Another simple example, but in this case we’ll use find on the Header object to obtain an iterator. Expected behavior for containers. begin() and end() Find returns an iterator. Expected behavior for containers. begin() and end() Find returns an iterator.

Transformations Transformations are stupidly hard in C Fundamentally all they do is consume data and produce data, the C++ API attempts to make it that simple. Remember: a TransformationPlugin is just a subclass of a TransactionPlugin so you can do anything you could do in a TransactionPlugin.

Transformations Transformations are chainable so a common pattern is InflateGzipTransformation ArbitraryContentTransform DeflateGzipTransformation

Transformation Example

Extends TransformationPlugin Because it’s a TransactionPlugin too it can add it’s own Hooks Transformations implement two methods consume and handleInputComplete Transformations have two methods available produce(std::string) and setOutputComplete() This simple example passes through all data and adds an HTML comment at the end

Transformations One common question regarding the interface is the manipulation of binary data when consume(const std::string &data) and produce(std::string) both operate with std::string. This is not a problem at all, just use std::string::c_str() or std::string::data() and std::string::length() To write binary data you would just do produce(std::string(binary_buffer, len)); OR std::string::assign(const char *, size_t) See GzipInfalteTransformation.cpp in lib/atscppapi/src

Async Loggers The C API made available TSTextLogObjects these are exposed via the Logger class. They also have additional functionality around log levels. Initialize the logger before you use it Two different built-in ways to log. Directly via the object or using the macros which will include FILE, FUNCTION, and LINE NUMBERS.

Stats ATS C APIs support stats which are available via traffic_line or http stats interface Like loggers you you must init() the stat. Set / get available on the stats Finally, you have increment / decrement available on the stat by one or arbitrary amounts.

Neat things you can do Custom response transaction plugin, you don’t need to use an intercept if you want to send a custom response - See lib/atscppapi/examples/customresponse/customresponse.cpp Note: the constructor reenables w/.error() so you should not.resume() after attaching this Plugin

Things that need to improve Async Mechanism – It’s currently overly complicated and difficult to use It’d be nice to remove the caching that exists around many of the internal structures (see the isInitialized() code scattered throughout). Any takers? I’m not sure why I thought this was a good idea…but I wrote it so blame me.

Things that would be great to add Generalizations around VIOs It would be ideal if we create a C++ API version of the VIOs and VConnections. – I think Daniel Morilha has done this in his latest plugin contribution.