Varnish always cache when certain cookie is present - varnish

I want a rule in my vcl_recv that checks if a certain cookie is present, and if so always serves a cached file (let's call this cookie VARNISH_USE_CACHE)
However, I also have a rule that runs after this to check for a session cookie (and if it exists always passes to apache), this rule should still run if VARNISH_USE_CACHE is not present.
I'm not quite sure how to check for one cookie and then check for another. I've tried assigning the cookies to a temp variable, and checking there. But I must not be understanding how vcl works, as when I do this I end up with the full cookie with nothing stripped out.
Here's my code in vcl_rec:
set req.http.tempCookie = ";" + req.http.Cookie;
set req.http.tempCookie = regsuball(req.http.tempCookie, ";(VARNISH_USE_CACHE)=", "; \1=");
set req.http.tempCookie = regsuball(req.http.tempCookie, ";[^ ][^;]*", "");
set req.http.tempCookie = regsuball(req.http.tempCookie, "^[; ]+|[; ]+$", "");
So I'm expecting req.http.tempCookie to be empty if there is no VARNISH_USE_CACHE, instead it always has all the cookies from req.http.Cookie.

Ah worked it out:
Forgot this line
set req.http.tempCookie = regsuball(req.http.tempCookie, "; +", ";");
So the code looks like:
set req.http.tempCookie = ";" + req.http.Cookie;
set req.http.tempCookie = regsuball(req.http.tempCookie, "; +", ";");
set req.http.tempCookie = regsuball(req.http.tempCookie, ";(VARNISH_USE_CACHE)=", "; \1=");
set req.http.tempCookie = regsuball(req.http.tempCookie, ";[^ ][^;]*", "");
set req.http.tempCookie = regsuball(req.http.tempCookie, "^[; ]+|[; ]+$", "");

Related

Error sending HTML mails with Agents in LotusScript

I am trying to send emails from agents, but i have an error always with the next document that the agent process.
For example, if there are 5 emails to send, the first is ok, but with the second the agent stops and show this error:
Objet variable not set
The code:
Sub Initialize
Dim ws As New NotesUIWorkspace
Dim ns As New NotesSession
Dim db As NotesDatabase
Dim docs As NotesDocumentCollection
Dim doc As NotesDocument
Dim body As NotesMIMEEntity
Dim maildoc As NotesDocument
Dim mh As NotesMIMEHeader
Dim stream As NotesStream
Dim formula As String
Dim header As String
Dim body2 As String
Set db = ns.CurrentDatabase
Set maildoc = db.Createdocument
'Set doc = ws.Currentdocument.Document
Set stream = ns.Createstream()
header = "Test Agent"
body2 = "Test Agent"
' Create the MIME headers
Set body = maildoc.CreateMIMEEntity
Set mh = body.CreateHeader("Subject")
' Dont convert text to rich text
ns.Convertmime = False
' Configure the mail
maildoc.Form = "Memo"
maildoc.Subject = "Test Agent"
maildoc.SendTo = "Email Destination"
maildoc.CopyTo = ""
maildoc.blindCopyTo = ""
Dim dateObj As New NotesDateTime("")
'#Adjust( fecha-hora ; años ; meses ; días ; horas ; minutos ; segundos ; [ DST ] )
formula = "(Form = ""frm-testdevelop-solicitud"") & (Estado = ""Borrador"")"
Set docs = db.Search(formula, Nothing, 0)
If docs.count > 0 Then
Print "hay " + Cstr(docs.count) + " docs"
Set doc = docs.GetFirstDocument
While Not doc Is Nothing
Call stream.Writetext(|<html lang="es">|)
Call stream.Writetext(|<head>|)
Call stream.Writetext(|<meta name="viewport" content="width=device-width, initial-scale=1.0">|)
Call stream.Writetext(|<meta http-equiv="X-UA-Compatible" content="ie=edge">|)
Call stream.Writetext(|</head>|)
Call stream.Writetext(|<body>|)
Call stream.Writetext(|<div style="background-color: #897d7d; border-radius: 0.5em; width: auto; height: 50px;">
<h1 style="color: white; text-align: center; padding: 0.1em;
font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;">| & header & |</h1>
</div>|)
Call stream.Writetext(|<p>| & body2 & |</p>|)
Call stream.Writetext(|</body>|)
Call stream.Writetext(|</html>|)
Call body.SetContentFromText(stream, {text/html;charset="utf-8"}, ENC_NONE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here is the error in the second iterate.
' Close the stream
Call stream.Close()
' Restore conversion
ns.Convertmime = True
' Send it
Call maildoc.Send(False)
' Next document
Set doc = docs.GetNextDocument(doc)
Wend
Else
Print "No hay documentos"
End If
Exit Sub
In the second iterate the agent stop in this part of the code:
Call body.SetContentFromText(stream, {text/html;charset="utf-8"}, ENC_NONE)
I know that the error is because I am sending this email with HTML format, because if I remove the sendemail block of code, the agents runs fine.
And I have no idea what I am doing wrong, any suggest?
Thanks a lot!
On every run you add the new content to the stream. As you do not "Open" a file with your stream, the "close" in your code does not do anything.
Replace the "stream.close" with a "stream.truncate" to remove the contents. That should do the trick.

Using SAPGUI in excel/VBA, Why does changing the .VerticalScrollbar.Position of a GuiTableControl breaks the object "connection"?

First my test code
Sub mytest2()
If Not IsObject(MyApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set MyApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = MyApplication.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(Wscript) Then
Wscript.ConnectObject session, "on"
Wscript.ConnectObject MyApplication, "on"
End If
Dim MyTableStr As String
Dim MyTable As Variant
MyTableStr = "wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1107/tabsTS_1100/tabpVGUE/ssubSUB_AUFTRAG:SAPLCOVG:3010/tblSAPLCOVGTCTRL_3010"
Set MyTable = session.findById(MyTableStr)
MyTable.VerticalScrollbar.Position = MyTable.VerticalScrollbar.Position + 1
Debug.Print MyTable.VerticalScrollbar.Position & " position"
This code will increase the table's vertical position and then fail on the debug.print line with error "Object doesn't support this action" Run-time error 445
If I add again the line
Set MyTable = session.findById(MyTableStr)
Between the .position line and the debug.print line, it works and the position gets printed.
Why does this happen ?
I found the answer in the SAP GUI scripting API pdf at
http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf
The important passage was in the description of the GuiButton control.
Function press
This emulates manually pressing a button. Pressing a button will always cause server communication to occur, rendering all references to elements below the window level invalid. The following code will therefore fail:
Set TextField = session.findById(".../txtF1")
session.findById(".../btnPB5").press
TextField.text = "Hello"
This is because when you execute commands, some will only affect the client state but other commands will cause communication with server and apparently when that happens, all previous object references are broken.
Changing the .VerticalScrollbar.Position of a GuiTableControl also causes communication with the server.

email with company domain

I'm trying to create a VBA script which uses the Google server but company domain. A typical example would be firstname#company.com.
I'm using the code below but it gives me an error if I use a domain name that's not gmail. I want to ask the user to enter their login information (username and password).
Sub SendGMail()
' Object creation
Set objMsg = CreateObject("CDO.Message")
Set msgConf = CreateObject("CDO.Configuration")
' Server Configuration
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = UserForm1.TextBox1.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = UserForm1.TextBox2.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
msgConf.Fields.Update
' Email
objMsg.To = UserForm1.TextBox1.Value
objMsg.From = UserForm1.TextBox1.Value
objMsg.Subject = "Test send with Gmail account"
objMsg.HTMLBody = "HTML/Plain text message."
objMsg.Sender = "Mr. Name"
Set objMsg.Configuration = msgConf
' Send
objMsg.Send
' Clear
Set objMsg = Nothing
Set msgConf = Nothing
End Sub
You need to add Microsoft CDO Reference before running the code
Tool-->Reference---> Microsoft CDO for Windows 2000 Library
Sub MailTest()
Dim Mail As New Message
Dim Config As Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 25
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = InputBox("EnterUsername")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox1.Text
Config(cdoSendPassword) = InputBox("Password")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox2.Text
Config.Fields.Update
Mail.To = "yahoo44#gmail.com"
Mail.From = Config(cdoSendUserName)
Mail.Subject = "Email Subject"
Mail.HTMLBody = "<b> mail body </b>"
Mail.Sender = " MRSender"
Mail.Send
MsgBox "mail has been sent"
End Sub

Set binary/base64 data as attachment in VB Script CDO.Message

I am trying to send out an email with attachment as PDF. I have my attachment data in binary/Base64, but when I am trying to write this to the stream, it is not working. Here is my code. I am using upload class from here - http://www.codeguru.com/csharp/.net/net_asp/article.php/c19297/Pure-ASP-File-Upload.htm
<!--METADATA TYPE="TypeLib" FILE="C:\windows\system32\cdosys.dll" -->
<!-- #include file="upload.asp" -->
<%
' Create the FileUploader
Dim Uploader, File
Set Uploader = New FileUploader
' This starts the upload process
Uploader.Upload()
Dim uploadFileName
Dim uploadFileData
' Check if any files were uploaded
If Uploader.Files.Count > 0 Then
' Loop through the uploaded files
For Each File In Uploader.Files.Items
uploadFileName = File.FileName
uploadFileData = File.FileData
Next
End If
Set ObjMailer = CreateObject("CDO.Message")
Set iConf = Server.CreateObject("CDO.Configuration")
Set Flds = iConf.Fields
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "localhost"
.Item(cdoSMTPServerPort) = 5000
.Item(cdoSMTPconnectiontimeout) = 10
.Update
End With
Set ObjMailer.Configuration = iConf
ObjMailer.Subject = "Subject"
ObjMailer.From = "from#testdomain.com"
ObjMailer.To = "to#testdomain.com"
ObjMailer.HTMLBody = "<h1>This is a sample html email body</h1>"
' Code to Attach the Email
Const cdoContentDisposition = "urn:schemas:mailheader:content-disposition"
Dim oPart : Set oPart = ObjMailer.Attachments.Add
oPart.ContentMediaType = "application/pdf"
oPart.Fields(cdoContentDisposition).Value = "attachment;filename=""Test.pdf"""
oPart.Fields.Update
Dim oStreamOut: Set oStreamOut = oPart.GetDecodedContentStream
oStreamOut.Type = 1
oStreamOut.Write uploadFileData
oStreamOut.Flush
ObjMailer.Send
Set ObjMailer = Nothing
Set iConf = Nothing
Set Flds = Nothing
If Err.number <> 0 Then
Response.Write "Error: " & Err.Number
Response.Write "Error (Hex): " & Hex(Err.Number)
Response.Write "Source: " & Err.Source
Response.Write "Description: " & Err.Description
End If
I get following error. I don't know where am I getting wrong.
ADODB.StreamDescription: Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
Can somebody help?
Finally, I got this working. Thanks to kul-tigin for pointing the issue for vbArray + vbByte. The data uploaded is multi-byte data and I need to normalize it.
Here is the function I got here to convert the multi-byte array.
Function GetFileDataBinary()
Dim RS, LMultiByte, Binary
Const adLongVarBinary = 205
Set RS = CreateObject("ADODB.Recordset")
LMultiByte = LenB(FileData)
If LMultiByte > 0 Then
RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte
RS.Open
RS.AddNew
RS("mBinary").AppendChunk FileData & ChrB(0)
RS.Update
Binary = RS("mBinary").GetChunk(LMultiByte)
End If
GetFileDataBinary = Binary
End Function
I changed my code like below and it worked just seemlessly.
Dim uploadFileName
Dim uploadFileData
' Check if any files were uploaded
If Uploader.Files.Count > 0 Then
' Loop through the uploaded files
For Each File In Uploader.Files.Items
uploadFileName = File.FileName
uploadFileData = File.GetFileDataBinary
Next
End If
I can call File.GetFileDataBinary because I put my GetFileDataBinary in my upload.asp.

IIS7 Response.WriteBuffer not working

We have an ASP.NET 1.1 application that uses Crystal Reports to spit out an excel spreadsheet. The codes works under IIS6 but when we try to migrate it to IIS7 it is spitting out html with no content instead of the Excel file.
The MIME Type exists. Below is the code we are using. I did not write this code as I'm working primarily in 3.5 framework now. My assumption is I am missing something in the IIS7 configuration not the code since it works on IIS6. The rest of the ASP.NET 1.1 application works on IIS7.
Dim cr As ReportClass
'EXPORT the report based on the export type passed in.
Dim ExpOptions As New ExportOptions
Dim ContentType As String
Dim strExt As String
Trace.Write("DisplayReport reportname=" + ReportName + " SQL=" + SQL + " SQLSub1=" + Convert.ToString(Session("SQLSub1")))
'Get the report filled with the data.
If Session("SQLSub1") <> "" Then
If Not Session("SubRptName") Is Nothing Then
cr = PopulateReport(GetReportObject(ReportName), SQL, Session("SQLSub1"), Session("SubRptName"))
Session("SQLSub1") = ""
Session("SubRptName") = Nothing
Else
cr = PopulateReport(GetReportObject(ReportName), SQL, Session("SQLSub1"))
Session("SQLSub1") = ""
End If
Else
cr = PopulateReport(GetReportObject(ReportName), SQL)
End If
If DisplayType = ReportType.Excel Then
If ReportName.ToUpper = "ACTION" Or ReportName.ToUpper = "INVENTORY_EXCEL" _
Or ReportName.ToUpper = "UNDERPERFORM" Or ReportName.ToUpper = "EMPLOYEE_EXCEL" Then
Dim excelFormatOpts As New ExcelFormatOptions
' Set the excel format options.
excelFormatOpts.ExcelTabHasColumnHeadings = True
excelFormatOpts.ExcelUseConstantColumnWidth = False
ExpOptions.FormatOptions = excelFormatOpts
Else
ExpOptions.FormatOptions = New ExcelFormatOptions
End If
ExpOptions.ExportFormatType = ExportFormatType.Excel
ContentType = "application/vnd.ms-excel"
strExt = ".xls"
ElseIf DisplayType = ReportType.PDF Then
ExpOptions.ExportFormatType = ExportFormatType.PortableDocFormat
ExpOptions.FormatOptions = New PdfRtfWordFormatOptions
ContentType = "application/pdf"
strExt = ".pdf"
End If
'Stream the report to the screen
Dim req As New ExportRequestContext
req.ExportInfo = ExpOptions
Dim s As Stream
Try
s = cr.FormatEngine.ExportToStream(req)
Catch ex As Exception
Trace.Warn("DisplayReport cr.FormatEngine.ExportToStream(req) failed: " + ex.Message)
Dim x As String = String.Empty
End Try
Response.Clear()
'Response.ClearHeaders()
'Response.ClearContent()
Response.Buffer = True
Response.ContentType = ContentType
Response.AddHeader("Content-Type", ContentType)
Dim buffer(s.Length) As Byte
s.Read(buffer, 0, Int(s.Length))
Response.BinaryWrite(buffer)
Dim strContentDisposition As String = "inline;filename=" & ReportName.ToString.ToLower & strExt.ToString
Trace.Write("DisplayReport strContentDisposition=" + strContentDisposition)
Response.AddHeader("Content-Disposition", strContentDisposition)
Response.Cache.SetMaxAge(New TimeSpan(0, 0, 10))
Response.End()
Asked some devs here at work, this is what I got so far:
"Never seen that before, I’ve never even used the export to stream option in crystal before. However, if I were to guess, I would look at server permissions as a possible fault. I’ve seen situations where the user has to have special privileges to access streams."

Resources