Handling IfModifiedSince requests with classic asp

Handles IfModifiedSince and sets Last-Modified date for any page

[ List of other entries | oyoy.eu main page ]
 

Usage:

I'm open to all corrections, additions or neat tricks. Feel free to contact me via the contact-link in the footer. The code could be compressed and adjusted to use more of asp.net - but for this example I want to keep it 'understandable'.

The code for the module Handle304.asp:

<script runat="server" language="vbscript">

Const GMTdiff = #01:00:00#

Sub SetPageModified(p_Response, p_Request, p_ChangeDate)
Dim strIfModified
strIfModified = p_Request.ServerVariables("HTTP_IF_MODIFIED_SINCE")

If strIfModified <> "" Then
Dim dtmIfModified
dtmIfModified = DateFromHTTP(strIfModified)

' give or take 5 minutes :-)
If (p_ChangeDate <= dtmIfModified + 0.0035) Then
p_Response.Clear()
p_Response.Status = "304 Not Modified"
p_Response.End()
Exit Sub
End If
End If

' add header to page
p_Response.AddHeader "Last-Modified", DateToHTTPDate(p_ChangeDate)

End Sub

Private Function DateToHTTPDate(p_Date)
' required: date in UTC
'Converts date (19991022 11:08:38)
'to HTTP form (Fri, 22 Oct 1999 12:08:38 GMT)
UseDate = p_Date + GMTdiff
DateToHTTPDate = EnglishWeekDayName(UseDate) & _
", " & Right("0" & Day(UseDate),2) & " " & EnglishMonthName(UseDate) & _
" " & Year(UseDate) & " " & Right("0" & Hour(UseDate),2) & _
":" & Right("0" & Minute(UseDate),2) & ":" & Right("0" & Second(UseDate),2) & " GMT"
End Function

Private Function DateFromHTTP(HTTPDate)
Dim strWeekday, strDay, strMonth
Dim strYear, strHour, strMinute, strSecond
Dim Out
Dim strHDate

'On Error Resume Next

strHDate = Trim(LCase(HTTPDate)) ' case independant

If right("---" & strHDate ,3) = "gmt" Then
' split into parts, assume correct build (otherwise error)
strWeekday = Mid(strHDate, 1, 3)
strDay = Mid(strHDate, 6, 2)
strMonth = Mid(strHDate, 9, 3)
strYear = Mid(strHDate, 13, 4)
strHour = Mid(strHDate, 18, 2)
strMinute = Mid(strHDate, 21, 2)
strSecond = Mid(strHDate, 24, 2)

' try to build a date
err.Clear
Out = DateSerial(strYear, MonthFromString(strMonth), strDay) + _
TimeSerial(strHour, strMinute, strSecond) + GMTDiff
If err <> 0 then
Out = DateSerial(1970,1,1) ' = error, components invalid
End If
Else
Out = DateSerial(1971,1,1) ' = error, date/time doesn't end with 'gmt'
End If
DateFromHTTP = Out
End Function

Function MonthFromString(strMonth)
Dim intMonthNr
Select Case strMonth ' assume lower case
Case "jan" : intMonthNr = 1
Case "feb" : intMonthNr = 2
Case "mar" : intMonthNr = 3
Case "apr" : intMonthNr = 4
Case "may" : intMonthNr = 5
Case "jun" : intMonthNr = 6
Case "jul" : intMonthNr = 7
Case "aug" : intMonthNr = 8
Case "sep" : intMonthNr = 9
Case "oct" : intMonthNr = 10
Case "nov" : intMonthNr = 11
Case "dec" : intMonthNr = 12
End Select
MonthFromString = intMonthNr
End Function

Private Function EnglishWeekDayName(p_Date)
Dim strWeekday
Select Case Weekday(p_Date, 1)
Case 1 : strWeekday = "Sun"
Case 2 : strWeekday = "Mon"
Case 3 : strWeekday = "Tue"
Case 4 : strWeekday = "Wed"
Case 5 : strWeekday = "Thu"
Case 6 : strWeekday = "Fri"
Case 7 : strWeekday = "Sat"
End Select
EnglishWeekDayName = strWeekday
End Function

Private Function EnglishMonthName(dt)
Dim strMonthName
Select Case Month(dt)
Case 1 : strMonthName = "Jan"
Case 2 : strMonthName = "Feb"
Case 3 : strMonthName = "Mar"
Case 4 : strMonthName = "Apr"
Case 5 : strMonthName = "May"
Case 6 : strMonthName = "Jun"
Case 7 : strMonthName = "Jul"
Case 8 : strMonthName = "Aug"
Case 9 : strMonthName = "Sep"
Case 10 : strMonthName = "Oct"
Case 11 : strMonthName = "Nov"
Case 12 : strMonthName = "Dec"
End Select
EnglishMonthName = strMonthName
End Function

</script>