Skip to content

Instantly share code, notes, and snippets.

@noplanman
Last active November 17, 2020 23:50
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save noplanman/eb360d6e82ec0d58a9da884bd4b279d1 to your computer and use it in GitHub Desktop.
Save noplanman/eb360d6e82ec0d58a9da884bd4b279d1 to your computer and use it in GitHub Desktop.
Upgrade nghttp2 for brew with a fix for older MacOS versions
#!/usr/bin/env bash
# Update brew repo.
printf "%s" "Updating brew repo..."
brew update &> /dev/null
echo "✅"
# Get the currently installed and new version of nghttp2.
printf "%s" "Fetch installed and stable nghttp2 versions..."
VERSION_INSTALLED="$(brew list --versions | grep nghttp2 | awk '{print $2}')"
VERSION_STABLE="$(brew info --json=v1 nghttp2 | sed -E -e 's/.*"stable":"([^"]*).*/\1/')"
echo "✅"
if [ "$VERSION_STABLE" = "$VERSION_INSTALLED" ]; then
echo "Latest version (${VERSION_STABLE}) seems to be installed already!";
read -r -p "Continue anyway? (Y/n): " REPLY
case "${REPLY:-y}" in
[yY] | [yY][Ee][Ss] ) ;;
*) echo "Cancelled ❌"; exit; ;;
esac
fi
if [ -z "$VERSION_INSTALLED" ]; then
echo "nghttp2 doesn't seem to be installed. Installing version ${VERSION_STABLE} now.";
fi
FILENAME="nghttp2--${VERSION_STABLE}.tar.xz"
FOLDERNAME="nghttp2-${VERSION_STABLE}"
FORMULAFILE="$(brew formula nghttp2)"
# Download the package.
printf "%s" "Downloading latest version of nghttp2 (${VERSION_STABLE})..."
brew fetch nghttp2 &> /dev/null
echo "✅"
# Change into the Homebrew cache folder.
cd "$(brew --cache)"
# Extract the archive.
printf "%s" "Extract the archive..."
tar xJf "${FILENAME}"
echo "✅"
# Apply the code changes from https://github.com/nghttp2/nghttp2/pull/1319
printf "%s" "Apply code fix..."
sed -ibkp 's/return dconn;/return std::move(dconn);/g' \
"${FOLDERNAME}/src/shrpx_client_handler.cc" \
"${FOLDERNAME}/src/shrpx_downstream_connection_pool.cc"
echo "✅"
# Compress the files to an archive and replace the original one.
printf "%s" "Create new archive..."
tar cJf ${FILENAME} ${FOLDERNAME} &> /dev/null
echo "✅"
# Get the checksum of the new archive.
SHASUM="$(sha256sum ${FILENAME} | awk '{print $1}')"
# Enter the new checksum in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/nghttp2.rb
printf "%s" "Inject new checksum..."
sed -ibkp "s/sha256 \".*\"$/sha256 \"${SHASUM}\"/g" \
"${FORMULAFILE}"
echo "✅"
# Install / Upgrade nghttp2.
ACTION=upgrade
if [ -z "$VERSION_INSTALLED" ]; then
ACTION=install
fi
printf "%s" "Starting nghttp2 ${VERSION_STABLE} ${ACTION}..."
brew $ACTION nghttp2 &> /dev/null
echo "✅"
# Reset the formula to prevent any repo update issues.
printf "%s" "Reset formula file..."
(cd "$(dirname $(brew formula nghttp2))" && git checkout "${FORMULAFILE}") &> /dev/null
echo "✅"
echo "All done 🎉"
@benjarlett
Copy link

Thanks :) worked for me with 10.12.5

@ak9
Copy link

ak9 commented Apr 6, 2020

Hi, with the latest version (nghttp2-1.40.0), there is an additional location requiring the same change:

sed -ibkp 's/return dconn;/return std::move(dconn);/g'
"${FOLDERNAME}/src/shrpx_downstream_connection_pool.cc"

After this (along-side the change in shrpx_client_handler), the package compiles.

Best,
Akshat

@noplanman
Copy link
Author

Thanks for pointing that out @ak9, I've not had any problem compiling though, even though shrpx_downstream_connection_pool.cc has had that line since forever.

Anyway, should it have caused problems for anyone, should be fixed now.

@typhonius
Copy link

typhonius commented Nov 17, 2020

Thanks this worked for me too (10.11.6), appreciate your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment