Google APIs Client Library for C++
html_scribe.h
Go to the documentation of this file.
00001 /*
00002  * \copyright Copyright 2013 Google Inc. All Rights Reserved.
00003  * \license @{
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *
00017  * @}
00018  */
00019 
00020 
00021 #ifndef APISERVING_CLIENTS_CPP_TRANSPORT_HTML_SCRIBE_H_
00022 #define APISERVING_CLIENTS_CPP_TRANSPORT_HTML_SCRIBE_H_
00023 
00024 #include <string>
00025 using std::string;
00026 
00027 #include "googleapis/client/transport/http_scribe.h"
00028 #include "googleapis/base/thread_annotations.h"
00029 #include "googleapis/base/integral_types.h"
00030 #include "googleapis/base/scoped_ptr.h"
00031 #include "googleapis/strings/strcat.h"
00032 #include "googleapis/strings/stringpiece.h"
00033 namespace googleapis {
00034 
00035 namespace client {
00036 class DataWriter;
00037 class HttpRequest;
00038 
00039 /*
00040  * Specialized HttpScribe that produces HTML transcripts.
00041  * @ingroup TransportLayerTesting
00042  *
00043  * The PresentationFlags enumeration allows you to control the structure of
00044  * the HTML produced. Depending on how you plan on browsing the HTML, the
00045  * choice can make it easier or harder due to the abstractions they control.
00046  *
00047  * If you want to copy and paste sequences of requests then you might want to
00048  * turn the all off. If you want to browse the sequence and only look at
00049  * header or playload details then you should just set those flags.
00050  *
00051  * You can use the base class scribe's max_snippet attribute to limit how
00052  * much request/response data you store for each request. Since everything
00053  * is going to be journaled into a single HTML document, this could be a good
00054  * idea if you are performing large media transfers!
00055  *
00056  * The implementation of this class may stream directly to the writer, in
00057  * which case it may not be well-formed HTML if it did not finish properly
00058  * (e.g. it is still scribing or the process crashed). If that is the case,
00059  * you may need to append the closing tags to make it well formed if you
00060  * need to have well formed HTML.
00061  */
00062 class HtmlScribe : public HttpEntryScribe {
00063  public:
00064   /*
00065    * Constructor.
00066    *
00067    * @param[in] censor The censor to use for scrubbing sensitive data.
00068    *                   The caller passes ownership.
00069    * @param[in] title For the HTML document title.
00070    * @param[in] writer Ownership is passed to the scribe. This writer will
00071    *            store the transcript.
00072    */
00073   HtmlScribe(
00074       HttpScribeCensor* censor, const StringPiece& title, DataWriter* writer);
00075 
00076   /*
00077    * Standard destructor.
00078    *
00079    * This will finish out the HTML and flush the writer to make it a
00080    * well-formed document.
00081    */
00082   virtual ~HtmlScribe();
00083 
00084   /*
00085    * Flushes the writer, but does not "finish out" the HTML to make
00086    * it well formed.
00087    */
00088   virtual void Checkpoint();
00089 
00090   enum PresentationFlags {
00091     EXPANDABLE_REQUEST = 0x1,
00092     EXPANDABLE_HEADERS = 0x2,
00093     EXPANDABLE_REQUEST_CONTENT = 0x4,
00094     EXPANDABLE_RESPONSE_BODY = 0x8,
00095     COLORIZE = 0x10,
00096     ALL = EXPANDABLE_REQUEST
00097           | EXPANDABLE_HEADERS
00098           | EXPANDABLE_REQUEST_CONTENT
00099           | EXPANDABLE_RESPONSE_BODY
00100           | COLORIZE
00101   };
00102 
00103   /*
00104    * Controls features in HTML output.
00105    *
00106    * @param[in] flags Bitwise-or of PresentationFlags enum values.
00107    */
00108   void set_presentation_flags(int flags) { presentation_ = flags; }
00109 
00110   /*
00111    * Returns the presentation flags.
00112    *
00113    * @return bitwise or value of individual enum values.
00114    *
00115    * @see PresentationFlags
00116    */
00117   int presentation_flags() const { return presentation_; }
00118 
00119   /*
00120    * Returns true if requests are expandable in the HTML.
00121    */
00122   bool expand_request() const {
00123     return presentation_ & EXPANDABLE_REQUEST;
00124   }
00125 
00126   /*
00127    * Returns true if headers are expandable in the HTML.
00128    */
00129   bool expand_headers() const  {
00130     return presentation_ & EXPANDABLE_HEADERS;
00131   }
00132 
00133   /*
00134    * Returns true if the request content is expandable in the HTML.
00135    */
00136   bool expand_request_content() const  {
00137     return presentation_ & EXPANDABLE_REQUEST_CONTENT;
00138   }
00139 
00140   /*
00141    * Returns true if the response body is expandable in the HTML.
00142    */
00143   bool expand_response_body() const {
00144     return presentation_ & EXPANDABLE_RESPONSE_BODY;
00145   }
00146 
00147  protected:
00148   /*
00149    * Returns an entry that produces the individual HTML transcript for
00150    * the request.
00151    */
00152   virtual Entry* NewEntry(const HttpRequest* request);
00153 
00154  private:
00155   int64 sequence_number_;
00156   scoped_ptr<DataWriter> writer_;
00157   string last_netloc_;
00158   int presentation_;
00159 };
00160 
00161 }  // namespace client
00162 
00163 } // namespace googleapis
00164 #endif  // APISERVING_CLIENTS_CPP_TRANSPORT_HTML_SCRIBE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines